循环选择下拉菜单5次

时间:2017-03-17 16:54:27

标签: ruby-on-rails ruby

基本上我希望用户能够从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

用户show.html.erb

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

edit.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 %>

错误消息:

  

用户中的ArgumentError#edit
  错误的参数数量(给定1,预期2..5)

1 个答案:

答案 0 :(得分:0)

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

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

因为select需要至少两个参数。我个人觉得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>