【问题标题】:undefined method 'map' for :id:Symbol:id:Symbol 的未定义方法 'map'
【发布时间】:2016-08-29 02:48:56
【问题描述】:
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 的用户时,只应显示该用户的挑战。

决斗/_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} %>'); 

duels_controller.rb

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

【问题讨论】:

  • collection_select 的第二个参数应该是method。并且集合应该作为第三个参数传递。检查apidock.com/rails/ActionView/Helpers/FormOptionsHelper/…
  • 你不应该在collection_select中指定一个对象来应用'select'标签吗? apidock.com/rails/ActionView/Helpers/FormOptionsHelper/…
  • 这样,&lt;%= collection_select(:challenge, :challenge_id, challenges, :id, :full_challenge, include_blank: true) %&gt;.?这消除了错误,但dropdown-no-2 仍在列出所有挑战,而不仅仅是针对相应用户的挑战。 @MishaSlyusarev
  • 您在控制器方法中传递name 参数。但是您正在使用 id 参数进行搜索。 User.find(params[:id]).
  • 你可能是对的,但无论哪种方式都行不通@Emu

标签: ruby-on-rails ruby error-handling


【解决方案1】:

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" %>

变成这样

<%= 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" %>

【讨论】:

  • 感谢您的建议 :) 尽管仍然列出了所有挑战,但问题仍然存在。
猜你喜欢
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 2015-07-13
  • 2012-09-11
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多