2017-03-17 74 views
0

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

用户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
错误的参数数目(1给出,预期2..5)

+0

您能够发布完整的错误消息,并且还你的模型,视图和控制器和应用程序目录结构中的每个文件的位置?我可能是错的,但我会期待你写的方法在控制器 – 2017-03-17 17:08:16

+0

@RailsKiddie更新它! – Amena

回答

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> 
+0

这很完美!谢谢 ! – Amena