Справка по вложенным формам - Как добавить существующие товары в новое объявление?

Мои модели кажутся правильно определенными, потому что я могу добавить столько продуктов в объявление через консоль.

МОДЕЛИ

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

КОНСОЛЬ

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 продукта в новую запись объявления через форму. Не уверен, что я делаю неправильно.


person Christian Fazzini    schedule 10.05.2011    source источник
comment
OOC - почему именно так настроены объявления_продуктов? почему бы не использовать has_and_belongs-to_many, который будет поддерживать для вас таблицу соединений?   -  person Taryn East    schedule 10.05.2011
comment
Потому что мне нужно больше работать над таблицей m:m   -  person Christian Fazzini    schedule 11.05.2011
comment
Итак, похоже, вам нужен реальный класс вместо этой таблицы соединений. Например, сообщение (например, объявление о продукте). Тогда Продукт имеет_много :коммуникаций, но, что наиболее важно, Коммуникация имеет_много :продуктов; has_many :announcements и then Product has_many :announcements :through =› :communication Тогда у Rails есть что-то стандартное для работы.   -  person Taryn East    schedule 15.05.2011


Ответы (1)


До сих пор я придумал это. В моей форме:

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

  def create
    @announcement = Announcement.new(params[:announcement])

    @announcement.products << Product.find_all_by_id(params[:products])

    respond_to do |format|
      if @announcement.save
         ...
      end
    ....

Это чистое решение? Или есть лучший способ сделать это?

person Christian Fazzini    schedule 11.05.2011