samedi 27 juin 2015

grouped_collection_select and has_many, through

I'm trying to setup a form for tasks where the user selects a game and the achievements that belong to it, following Ryan Bates' tutorial. The only difference between that tutorial and what I'm working on is that I have a has_many through association in my Task model.

In the form I seem to get this error on the line for my grouped_collection_select:

undefined method 'achievement_id' for #<Task:0x007fe4da7e8cd8>

Models

class Game < ActiveRecord::Base
  has_many :achievements
  has_many :tasks
end

class Task < ActiveRecord::Base
  belongs_to :game
  has_many :achievements, through: :game
end

class Achievement < ActiveRecord::Base
  belong_to :game
end

Tasks Form

<%= form_for(@task) do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :game_id %><br>
    <%= f.collection_select :game_id, Game.order(:title), :id, :title, include_blank: true %>
  </div>
  <div class="field">
    <%= f.label :achievement_ids %><br>
    <%= f.grouped_collection_select :achievement_id, Achievement.order(:title), :achievement, :title, :id, :title, include_blank: true, multiple: true %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Aucun commentaire:

Enregistrer un commentaire