accepts_nested_attributes_for how, example

37 views
Skip to first unread message

Srdjan Cengic

unread,
Nov 10, 2013, 6:24:16 PM11/10/13
to rubyonra...@googlegroups.com
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/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for
but can't get it how to work.

Colin Law

unread,
Nov 11, 2013, 6:11:34 AM11/11/13
to rubyonra...@googlegroups.com
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
Message has been deleted

Srdjan Cengic

unread,
Nov 11, 2013, 2:17:08 PM11/11/13
to rubyonra...@googlegroups.com
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 %>

Reply all
Reply to author
Forward
0 new messages