循环选择下拉菜单 5 次



基本上,我希望用户能够从5个不同的下拉菜单中选择他们的前5项技能,这些下拉菜单稍后将在用户的显示页面上查看

用户控制器:

class UsersController < ApplicationController
      def new
            @user = User.new
          end
          def show
            @user = User.find(params[:id])
            @events = @user.events
          end
          def create
            @user = User.new(user_params)
            if @user.save
              session[:user_id] = @user.id
              redirect_to root_url
            else
              render "new"
            end
          end
          def edit
            @user = current_user
          end
          def update
            @user = current_user
            @user.update_attributes(user_params)
            if @user.save
              redirect_to user_path(current_user)
            else
              render "edit"
            end
          end
          def upload
            uploaded_io = params[:user][:picture]
            File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
              file.write(uploaded_io.read)
            end
          end

          private
          def user_params
            params.require(:user).permit(:email, :password, :password_confirmation, :first_name, :last_name, :region, :volunteer_position, :summary, :date_of_birth, :picture, :hours, :org_admin, :skills, :goals)
          end
        end

    users model:
          SKILLS = ["","Administrative", "Analytical", "Artistic/Creative", "Budgeting",
          "Communicaton", "Computer", "Conflict Resolution", "Creating Ideas",
          "Creating Procedures", "Creating New Solutions", "Customer Service",
          "Decision Making", "Fundraising", "Handling Complaints", "Innovative",
          "Leadership", "Learning", "Logical Thinking", "Maintaining High Levels of Activity",
          "Negotiating", "Networking", "Organizational", "Planning", "Problem Solving",
          "Reporting", "Team Work", "Technical", "Time Management", "Training"]

          def select_skills
            @skills = []
            5.times do |i|
              return i.skills
            end
          end

用户显示.html.erb

<tr>
    <td>skills:</td>
    <td><%= current_user.skills %></td>
</tr>

编辑.html.erb

 <tr>
     <td> User Goals </td>
       <% select User::SKILLS.each do |skill|%>
       <% @skill = Skills.order.first(5) %>
 <tr>
   <th><%= current_user.skills %></th>
 </tr>
 <% end %>

错误消息:

用户中的参数错误#编辑
错误的参数数(给定 1,预期 2..5(

您在此代码片段上收到参数错误:

<tr>
  <td> User Goals </td>
  <% select User::SKILLS.each do |skill|%>
  <% @skill = Skills.order.first(5) %>
</tr>

因为选择至少需要两个参数。我个人认为select_tag更直接:

<tr>
  <td> User Goals </td>
  <%= select_tag "skills", options_for_select(User::SKILLS) %>
</tr>

这将为您提供所有技能的单一选择。您可以通过循环轻松进行 5 个选择:

<tr>
  <td> User Goals </td>
  <% 5.times do |i| %>
    <%= select_tag "skills<%=i%>", options_for_select(User::SKILLS) %>
  <% end %>
</tr>

最新更新