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.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/76302b6f-b257-440f-862c-1f11ea8929e7%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
On 10 November 2013 23:24, Srdjan Cengic <cengasrle-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvLEZjCqj%2BY_y98Qx%2BrnC6vQJVtctqHY_%3D2i%3DrYECJ7%2BQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
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.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/ea8b1ea2-dc6b-4601-9b84-73c7a7621a7e%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
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 %>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/541c49d4-b539-4adc-abb5-09ddd7fbe421%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Apparently Analagous Threads
- Link to remote and the equivalent in Rails 3
- accepts_nested_attributes_for :reject_if issue
- nested_form gem don't work for accepts_nested_attributes_for :limit
- has_one accepts_nested_attributes_for fields_for NOT WORKING HELP
- accepts_nested_attributes_for vs. multiple controllers on one page