문제

I am trying to create a nested attribute form to create a model which is primarily an association "connector" between two other models. In my case, the models represent books, awards, and the "connector" model book_awards. When I am editing a book, I want to be able to quickly select which awards it has won.

I've been using http://railscasts.com/episodes/196-nested-model-form-part-1 to help me get started, but I'm afraid I'm pretty much stuck.

Another SO question which seems similar is accepts_nested_attributes_for with find_or_create? Unfortunately, it's also not quite what I'm doing and I haven't been able to adapt it.

My models look like this. Each model has additional attributes and validations etc, but I've removed them for clarity.

class Book < ActiveRecord::Base
  has_many :book_awards
  accepts_nested_attributes_for :book_awards, :allow_destroy => true
end

class Award < ActiveRecord::Base
  has_many :book_awards
end

class BookAward < ActiveRecord::Base
  belongs_to :book, :award
end

In my book controller methods for edit and new, and the failure cases for create and update I have a line @awards = Award.all.

In my view, I would like to see a list of all awards with check boxes next to them. When I submit, I would like to either update, create, or destroy a book_award model. If the check box is selected, I would like to update an existing model or create a new one if it doesn't exist. If the check box isn't selected, then I would like to destroy an existing model or do nothing if the award never existed. I have a partial for book_awards. I'm not sure if the check box selector should be in this partial or not.

I think my check box will be my hook to :_destroy but with its polarity reversed. I think something like this will basically do it:

= f.check_box :_destroy, {}, 0, 1

Currently, I have this in my partial but I'm not sure where it really belongs.

Next comes my view which currently doesn't work, but maybe it will help demonstrate what I'm trying to do. I loop through the awards and use a fields_for to set nested attributes for anything that already exists. It's horribly ugly, but I think it somewhat works. However, I don't really know how to get started with the else case.

= f.label :awards
- @awards.each do |a|
  - if f.object.awards && f.object.awards.include?(a)
    = f.fields_for :book_awards, f.object.book_award.select{|bas| bas.award == a } do |ba|
      = render 'book_awards', :f => ba, :a => a
  - else
    = fields_for :book_awards do |ba|
      = render 'book_awards', :f => ba, :a => a

I would prefer the awards to be listed in the same order each time (my @awards assignment in the controller will probably specify the order) as opposed to listing the existing awards first or last.

도움이 되었습니까?

해결책

I hate to answer my own question, but I finally figured out something which works. The first thing I needed to do was to update the "new" case based on the crazy object which was included in the railscast. Next, I needed to manually set the :child_index. Finally, I needed to manually set the :_destroy check box appropriately.

.field
  = f.label :awards
  - @awards.each_with_index do |a,i|
    - if exists = (f.object.awards && f.object.awards.include?(a))
      - new_ba = f.object.book_awards.select{|s| s.award == a}
    - else
      - new_ba = f.object.class.reflect_on_association(:book_awards).klass.new
    = f.fields_for :book_awards, new_ba, :child_index => i do |ba|
      = render 'book_awards', :f => ba, :a => a, :existing => exists

My partial looks like this:

.field
  = f.check_box :_destroy, {:checked => existing}, 0, 1
  = f.label a.name
  = f.hidden_field :award_id, :value => a.id
  = f.label :year
  = f.number_field :year

It's not horribly pretty, but it seems to do exactly what I wanted.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top