Note that there are some explanatory texts on larger screens.

plurals
  1. POcreate new object in rails3 using multimodel form
    primarykey
    data
    text
    <p>I am n00b as rails is concerned. I am trying yo create a single multimodel form in my first rails3 project. Details are given below:</p> <pre><code>class Item &lt; ActiveRecord::Base # attr_accessible :title, :body has_many :item_reviews, :dependent =&gt; :destroy accepts_nested_attributes_for :item_reviews end </code></pre> <p>and</p> <pre><code>class ItemReview &lt; ActiveRecord::Base # attr_accessible :title, :body belongs_to :item end </code></pre> <p>So as clear, an item can have multiple reviews but when I am creating an item, I want at least 1 review for it. So I want to get item and first review in single form while item creation.</p> <p>I am using following view:</p> <pre><code>&lt;%provide(:title,'Create')%&gt; &lt;h1&gt; Add an Item review&lt;/h1&gt; &lt;div class="row"&gt; &lt;div class="span6 offset3"&gt; &lt;%= form_for (@item) do |f| %&gt; &lt;%= f.label :name %&gt; &lt;%= f.text_field :name %&gt; &lt;% f.fields_for :item_reviews, @item.item_reviews do |ff| %&gt; &lt;%= ff.label :shop_address %&gt; &lt;%= ff.text_field :shop_address %&gt; &lt;% end %&gt; &lt;%= f.submit "Submit", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p><code>&lt;% f.fields_for :item_reviews, @item.item_reviews do |ff| %&gt;</code> will not work because there is not item_review associated with <code>@item</code> currently (<code>@item = Item.new</code>) Until I save @item, I can't create new item_review. What should I do in that case.</p> <p>I know one possibility is model independent form but can't I use something above to make life easy.</p> <p><strong>PS:</strong> I am using bootstrap, just in case if that helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload