Forum: Ruby on Rails accepts_nested_attributes_for how, example

236dd7ca555806d7c971f1af1523a3d5?d=identicon&s=25 Srdjan Cengic (Guest)
on 2013-11-11 00:25
(Received via mailing list)
I have following tables with following models:
       users(id, role_id) *has_many :entries*
       categories(id, category_name) *has_many :entries*
       entries(id, category_id, user_id)  *belongs_to :user, belongs_to
:category, has_one :storage*
       storages(id, title, content, entry_id)* belongs_to :entry*,
*has_one
:vote*
       votes(id, count, storage_id) *belongs_to :storage*

Now, when user create new entry(through form) few things must happen:
      - New row in entry table will be created(with *already
existing*user_id and category_id)
      - New row in storages table will be created(title and content of
the
post with above entry_id)
      - New row in votes table(with above storage_id, count is default
0)

Form for creating new entry have, combo box for selecting category(like:
"Pictures", "Movies", etc), text box for title and text-area for
content.

I followed many tutorials and documentation like:
http://railscasts.com/episodes/196-nested-model-form-part-1 and
http://api.rubyonrails.org/classes/ActiveRecord/Ne...
but can't get it how to work.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-11-11 12:13
(Received via mailing list)
On 10 November 2013 23:24, Srdjan Cengic <cengasrle@gmail.com> wrote:
>       - New row in entry table will be created(with already existing user_id
>
http://api.rubyonrails.org/classes/ActiveRecord/Ne...
> but can't get it how to work.

First, I presume you have worked right through a good tutorial such as
railstutorial.org.  If not then go and do that first, then you may be
able to answer the questions yourself.

If you have already done that then there is no point describing the
whole problem area with a long list of things you need to happen and
then just say you can't get it to work.  Start with just one of the
things you want to do and get that working, then add the next
requirement.  If you get stuck then come back with a description of
just the bit you can't get working, show us your code (for the bit
that does not work) and someone will surely help.

Colin
236dd7ca555806d7c971f1af1523a3d5?d=identicon&s=25 Srdjan Cengic (Guest)
on 2013-11-11 19:06
(Received via mailing list)
Thanks Colin on reply. I tried with following code, which work good but
i
don't know how to create row in *votes* table.

*// routes.rb*
resources :entries, :only =>[:new, :create]

//* entries_controller.rb*
class EntriesController < ApplicationController
  def new
    @entry = Entry.new(*:user_id => 1*)
    @entry.build_storage
  end

  def create
    Entry.create!(params[:entry].permit(:user_id, :category_id,
:storage_attributes => [:title, :content]))
    redirect_to root_url
  end
end

// *entry.rb*
class Entry < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    has_one :storage

    *accepts_nested_attributes_for :storage*
end

// *new.html.erb*
<%= form_for @entry do |f| %>
    <%= f.select :category_id, options_for_select([["Pictures", "1"],
["Status", "2"]]) %>
    <%= *f.hidden_field :user_id, :value => @entry.user_id* %>

    <%= f.fields_for :storage do |storage| %>
        <p>
            <%= storage.label :title %><br>
            <%= storage.text_field :title %>
        </p>
        <p>
            <%= storage.label :content %><br>
            <%= storage.text_area :content %>
        </p>
    <% end %>

    <%= f.submit "Submit" %>
<% end %>

So now i want to add new row in votes also(votes(id, count, storage_id),
count default = 0).
I assume here i dont need *accepts_nested_attributes_for *because i dont
need to enter anything from form for votes.
236dd7ca555806d7c971f1af1523a3d5?d=identicon&s=25 Srdjan Cengic (Guest)
on 2013-11-11 20:18
(Received via mailing list)
Thanks Colin on reply. I tried with following code, which work good so
just
wanted to ask is this correct solution, for some reason i think this
code
"smell".

*// routes.rb*
resources :entries, :only =>[:new, :create]

*// user.rb*
class User < ActiveRecord::Base
    has_many :entries
end

*// category.rb*
class Category < ActiveRecord::Base
    has_many :entries
end

*// entry.rb*
class Entry < ActiveRecord::Base
    belongs_to :user
    belongs_to :category
    has_one :storage

    accepts_nested_attributes_for :storage
end

*// storage.rb*
class Storage < ActiveRecord::Base
    belongs_to :entry
    has_one :vote
end

*// vote.rb*
class Vote < ActiveRecord::Base
    belongs_to :storage
end

// *entries_controller.rb*
class EntriesController < ApplicationController
  def new
    @entry = Entry.new(:user_id => current_user.id) * #Pass this to
new.html.erb to use it like hidden field*
    @entry.build_storage
  end

  def create
    new_entry = Entry.new(params[:entry].permit(:user_id, :category_id,
:storage_attributes => [:title, :content]))
    new_entry.storage.build_vote *# Create new row in votes table*
    new_entry.save

    redirect_to root_url
  end
end

*// views/entries/new.html.erb*
<%= form_for @entry do | f | %>
    <%= f.select :category_id, options_for_select([["slike", "1"],
["statusi", "2"]]) %>
    <%= *f.hidden_field :user_id, :value => @entry.user_id* %> *#Now
pass
this to create action*

    <%= f.fields_for :storage do |storage| %>
        <p>
            <%= storage.label :title %><br>
            <%= storage.text_field :title %>
        </p>
        <p>
            <%= storage.label :content %><br>
            <%= storage.text_area :content %>
        </p>
    <% end %>

    <%= f.submit "Submit" %>
<% end %>
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.