Note that there are some explanatory texts on larger screens.

plurals
  1. POMass-assign attributes fails in nested form. Silent error?
    text
    copied!<p>There are several questions here that <a href="https://stackoverflow.com/questions/4525962/how-do-i-make-nested-attributes-work-in-rails3">cover</a> <a href="https://stackoverflow.com/questions/4579279/accepts-nested-attributes-for-causing-sqlexception">this</a> <a href="https://stackoverflow.com/questions/4381897/accepts-nested-attributes-not-saving-any-changes">already</a>, I know. I am new to programming and rails, so please bear with me. My goal is to collect <code>n</code> tag objects and display them in my show and index actions.</p> <p><strong>UPDATE</strong> Thanks to both guys who answered. Each suggestion nudged me in the right direction. I am able to get the rake task to create posts by passing in an empty array to initialize the <code>tags</code> object. However tags are still not created. On further inspection I get the following SQL exception:</p> <pre><code>irb(main):002:0&gt; u.posts.build(title: "a new day", tags: "jump") WARNING: Can't mass-assign protected attributes: tags (1.7ms) SELECT 1 FROM "posts" WHERE "posts"."title" = 'a new day' LIMIT 1 (0.5ms) COMMIT =&gt; #&lt;Post id: nil, title: "a new day", description: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil&gt; </code></pre> <p>My set up is as follows:</p> <p><code>Tag</code> Model</p> <pre><code>class Tag &lt; ActiveRecord::Base belongs_to :post end </code></pre> <p><code>Post</code> Model</p> <pre><code>class Post &lt; ActiveRecord::Base has_many :tags, autosave: true attr_accessible :title, :description, :content, :tags_attributes accepts_nested_attributes_for :tags, allow_destroy: true, reject_if: lambda {|attrs| attrs.all? {|key, value| value.blank?}} #add n number of form fields to capture tags in each article. def with_blank_tags(n = 3) n.times do tags.build end self end end </code></pre> <p>'View' code</p> <pre><code>&lt;%= form_for(@post.with_blank_tags) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.fields_for(:tags) do |tags| %&gt; &lt;%= unless tags.object.new_record? tags.check_box('_destroy') + tags.label('_destroy', 'Remove Tag') end%&gt; &lt;%= tags.label :tags, "Add a Tag"%&gt; &lt;%= tags.text_field :tags %&gt; &lt;%end%&gt; &lt;/div&gt; &lt;%end%&gt; </code></pre> <p>'Controller' code</p> <pre><code>def new @post = @user.posts.build end def create @post = @user.posts.build(params[:post]) if @post.save? respond_to do |format| format.html { redirect_to @post, notice: 'Post was successfully created.' } else format.html { render action: :new } end end end </code></pre> <p>My rake task:</p> <pre><code>namespace :db do desc "Fill database with sample data" task :posts =&gt; :environment do Rake::Task['db:reset'].invoke make_users make_posts end end def make_users puts "making users..." 5.times do |n| name = Faker::Name.name password = "foo" email = "example-#{n+1}@example.com" @user=User.create!( codename: name, email: email, password: password, password_confirmation: password) end end def make_posts puts "making posts..." User.all(:limit =&gt; 3).each do |user| 10.times do content = Faker::Lorem.paragraphs(3) description = Faker::Lorem.words(10) title = Faker::Lorem.words(4) tag = [] post = user.posts.create!(title: title, description: description, content: content, tags_attributes: tag) end end end </code></pre>
 

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