2016-08-29 19 views
0
ActionView::Template::Error (undefined method `map' for :id:Symbol): 
    # duels/_user_challenges.html.erb 
    1: <div id="dropdown-no-2"> 
    2: <%= collection_select 'challenge_id', challenges, :id, :full_challenge, include_blank: true %>. 
    3: </div> 

ユーザーがduelにユーザーを選択すると、それぞれのユーザーのチャレンジのみが表示されます。未定義のメソッド 'map' for:id:Symbol

決闘/ _dueler_fields.html.erb

<%= f.collection_select :user_id, User.order(:name),:id, :full_name, include_blank: true, id: "id_of_the_user_id_select_box" %> 
    will 

<%= render :partial => 'user_challenges', locals: {challenges: Challenge.order(:deadline)} %> 

<script> 
    $("#id_of_the_user_id_select_box").change(function() { 
     $.ajax({ 
      type: "GET", 
      url: '<%= user_challenges_path %>', 
      data: {name: $('#id_of_the_user_id_select_box').prop('value')} 
     }); 
    }); 
</script> 

決闘/ user_challenges.js.erb

$("#dropdown-no-2").html('<%=j render :partial => "user_challenges", locals: {challenges: @challenges} %>'); 

def user_challenges 
    @user = User.find(params[:id]) 
    @challenges = @user.challenges.order(:deadline) 
end 
+0

になります。コレクションは3番目のパラメーターとして渡す必要があります。チェックしてください。http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select –

+0

collection_selectに 'select'タグを適用するオブジェクトを指定するべきではありませんか? http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/collection_select –

+0

このように、<%= collection_select(:challenge、:challenge_id、チャレンジ、:id、:full_challenge、include_blank:true)%>。これでエラーは解消されましたが、 'dropdown-no-2'は依然として、それぞれのユーザーの課題だけでなく、すべての課題をリストしています。 @MishaSlyusarev –

答えて

1
duels_controller.rb

http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-selectを参照してください。

私はこれを達成するためにselect FormHelperのを使用することをお勧め:

だから、この

<%= f.collection_select :user_id, User.order(:name),:id, :full_name, include_blank: true, id: "id_of_the_user_id_select_box" %> 

がcollection_select` `の2番目のパラメータが` method`する必要があります。この

<%= f.select :user_id, User.order(:name).map { |user| [user.full_name, user.id] }, include_blank: true, id: "id_of_the_user_id_select_box" %> 
+0

その提案をありがとう:)問題は引き続きすべての課題に記載されていますが、問題は続きます。 –

関連する問題