Nested forms help - How to add existing products to a new announcement?
My models seem correctly defined, because I can add as many products to an Announcement via console.
MODELS
class Product < ActiveRecord::Base
has_many :announcement_products
has_many :announcements, :through => :announcement_products
accepts_nested_attributes_for :announcements#, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
class Announcement < ActiveRecord::Base
开发者_JAVA百科 has_many :announcement_products
has_many :products, :through => :announcement_products
accepts_nested_attributes_for :products#, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
class AnnouncementProduct < ActiveRecord::Base
belongs_to :announcement
belongs_to :product
end
CONSOLE
a = Announcement.new()
a.products << Product.first
a.products << Product.last
a.name = 'foo'
a.description = 'bar'
a.save!
However, I am trying to create an announcement and add a product via a select box in a form and I get an error:
undefined method `product_id' for #<Announcement:0x00000103c1d7d0>
I tried:
THE FORM
<% form_for([:admin, @announcement], :html => { :multipart => true }) do |f| %>
<% if @announcement.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@announcement.errors.count, "error") %> prohibited this announcement from being saved:</h2>
<ul>
<% @announcement.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<ul>
<li class="clearfix">
<%= f.label :attachment, 'Announcement image' %>
<div class="file_field">
<button><!-- this is skinnable --></button>
<%= f.file_field :attachment %>
</div>
<p id="file_name"><%= @announcement.attachment.blank? ? '' : File.basename(@announcement.attachment.url) %></p>
<% unless @announcement.attachment.blank? %>
<%= image_tag @announcement.attachment.url, :class => 'attachment_image' %>
<% end %>
</li>
<li class="clearfix">
<%= f.label :name %>
<%= f.text_field :name %>
</li>
<li class="clearfix mark_it_up">
<%= f.label :description %>
<%= f.text_area :description %>
</li>
<li class="clearfix">
<%= f.label :product_id %>
<%= f.collection_select :product_id, Product.all, :id, :name, { :prompt => 'Select a product' } %>
<span><%= link_to 'add', new_admin_product_path %></span>
</li>
....
I'm trying to add 2 products to a new Announcement record via the form. Not sure what I am doing wrong.
So far, I have come up with this. In my form:
<% form_for([:admin, @announcement], :html => { :multipart => true }) do |f| %>
<% if @announcement.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@announcement.errors.count, "error") %> prohibited this announcement from being saved:</h2>
<ul>
<% @announcement.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<ul>
<li class="clearfix">
<%= f.label :attachment, 'Announcement image' %>
<div class="file_field">
<button><!-- this is skinnable --></button>
<%= f.file_field :attachment %>
</div>
<p id="file_name"><%= @announcement.attachment.blank? ? '' : File.basename(@announcement.attachment.url) %></p>
<% unless @announcement.attachment.blank? %>
<%= image_tag @announcement.attachment.url, :class => 'attachment_image' %>
<% end %>
</li>
<li class="clearfix">
<%= f.label :name %>
<%= f.text_field :name %>
</li>
<li class="clearfix mark_it_up">
<%= f.label :description %>
<%= f.text_area :description %>
</li>
<li class="clearfix">
<%= select_tag 'products[]', options_from_collection_for_select(Product.all, 'id', 'name'), :id => nil, :class => 'products' %>
<%= select_tag 'products[]', options_from_collection_for_select(Product.all, 'id', 'name'), :id => nil, :class => 'products' %>
</li>
....
Then, in my controller, def create method:
def create
@announcement = Announcement.new(params[:announcement])
@announcement.products << Product.find_all_by_id(params[:products])
respond_to do |format|
if @announcement.save
...
end
....
Is this a clean solution? Or is there a better way to do this?
精彩评论