嵌套表单帮助 - 如何将现有产品添加到新公告?

时间:2011-05-10 08:50:11

标签: ruby-on-rails nested-forms

我的模型似乎已正确定义,因为我可以通过控制台向公告添加任意数量的产品。

模型

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
  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!

但是,我正在尝试创建公告并通过表单中的选择框添加产品,我收到错误消息:

undefined method `product_id' for #<Announcement:0x00000103c1d7d0>

我试过了:

表格

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

我正在尝试通过表单将2个产品添加到新的公告记录中。不确定我做错了什么。

1 个答案:

答案 0 :(得分:0)

到目前为止,我已经想出了这个。在我的表单

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

然后,在我的控制器中,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
    ....

这是一个干净的解决方案吗?或者有更好的方法吗?