ActionView::Template::Error (undefined method `map' for :id:Symbol):
1: <div id="dropdown-no-2">
2: <%= collection_select 'challenge_id', challenges, :id, :full_challenge, include_blank: true %>.
3: </div>
如何解决此错误,以便仅列出该用户的挑战?
duels / _dueler_fields.html.erb
<%= f.select :user_id, User.order(:name).map { |user| [user.full_name, user.id] }, include_blank: true, id: "change-challenge-options" %>
will
<%= render :partial => 'user_challenges', locals: {challenges: Challenge.order(:created_at)} %>
<script>
$( "#change-challenge-options" ).change(function() {
$.ajax({
type: "GET",
url: '<%= user_challenges_path %>',
data: {name: $('#change-challenge-options').prop('value')}
});
});
</script>
duels / _user_challenges.html.erb
<div id="dropdown-no-2">
<%= collection_select(':challenge_id', challenges, :id, :full_challenge, include_blank: true) %>.
</div>
duels / user_challenges.html.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(:created_at)
end
def new
@duel = Duel.new
@duel.duelers << Dueler.new(user_id: current_user.id, user_name: current_user.name, user_last_name: current_user.last_name)
respond_with(@duel)
end
routes.rb
get 'duels/user_challenges', :to => 'duels#user_challenges', as: 'user_challenges'
最佳答案
更改为:
<%= collection_select(:duel, :challenge_id, challenges, :id, :full_challenge, include_blank: true) %>
请参阅
collection_select