Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding complex forms and relationships
    primarykey
    data
    text
    <p>Post Model</p> <pre><code>class Post &lt; ActiveRecord::Base attr_accessible :user_id, :title, :cached_slug, :content belongs_to :user has_many :lineitems def lineitem_attributes=(lineitem_attributes) lineitem_attributes.each do |attributes| lineitems.build(attributes) end end </code></pre> <p>Post View:</p> <pre><code>&lt;% form_for @post do |f| %&gt; &lt;%= f.error_messages %&gt; &lt;p&gt; &lt;%= f.label :title %&gt;&lt;br /&gt; &lt;%= f.text_field :title %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :cached_slug %&gt;&lt;br /&gt; &lt;%= f.text_field :cached_slug %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :content %&gt;&lt;br /&gt; &lt;%= f.text_area :content, :rows =&gt; 3 %&gt; &lt;/p&gt; &lt;% for lineitem in @post.lineitems %&gt; &lt;% fields_for "post[lineitem_attributes][]", lineitem do |lineitem_form| %&gt; &lt;p&gt; Step: &lt;%= lineitem_form.text_field :step %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;p&gt;&lt;%= f.submit %&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <p>From the controller</p> <pre><code> 12 def new 13 @user = current_user 14 @post = @user.posts.build(params[:post]) 15 3.times {@post.lineitems.build} 16 end 17 18 def create 19 debugger 20 @user = current_user 21 @post = @user.posts.build(params[:post]) 22 if @post.save 23 flash[:notice] = "Successfully created post." 24 redirect_to @post 25 else 26 render :action =&gt; 'new' 27 end 28 end </code></pre> <p>I am currently playing with some code and watching railscasts. I am on 73 and have a question about saving this form.</p> <p>I have pasted some code and while following <a href="http://media.railscasts.com/videos/073_complex_forms_part_1.mov" rel="nofollow">railscasts 73</a>. My code is a bit different at around line 20 to 23 regarding another post relationship. Using debugger, @post only has the user_id and the post values. params contains the lineitem_attributes. The lineitems do not get saved.</p> <p>How do I build the post including the lineitems?</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.
    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