Hi - I''m trying to figure out how to group nested fields in the form
such that they all submit with a certain order. I followed the complex
forms railscast (http://railscasts.com/episodes/75), but I''m trying to
submit a nested model with more than a single field.
<% fields_for ''project[task_attributes][]'', task do |tf|
%>
<p>Name: <%= tf.text_field :name, :index => nil %></p>
<p>Start: <%= tf.text_field :start, :index => nil %></p>
<p>End: <%= tf.text_field :end, :index => nil %></p>
<% unless task.new_record? %>
<% tf.hidden_field :id, :index => nil %>
<% end %>
<% end -%>
When I submit, params looks strange and is completely disorganized:
{ "project" =>
{ "name" => "test",
"task_attributes" => [{ "start" =>
"10:00" },
{ "start" => "12:00" },
{ "end" => "10:30",
"start" => "11:00" },
{ "end" => "11:30" },
{ "end" => "12:30",
"id" => "1" },
{ "id" => "2", "name"
=> "mow lawn" },
{ "name" => "buy milk" },
{ "name" => "clean room" }] }}
If I ommit the :index => :nil, I get an error "conflicting types for
parameter containers. Expected an instance of Array but found an
instance of Hash" called on task_attribues. What I need is exactly that
- a hash grouping each task by id with it''s attributes:
{ "task_attributes" =>
{ "1" => { "name" => ''buy milk'',
"start" => ''10:00'',
"end" => ''10:30'' }
"2" => {..}...}
I''m feeling like I''m missing something big here... Any help
will be
great - thanks.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On 23 Feb 2009, at 15:57, sa 125 wrote:> > When I submit, params looks strange and is completely disorganized: > > { "project" => > { "name" => "test", > "task_attributes" => [{ "start" => "10:00" }, > { "start" => "12:00" }, > { "end" => "10:30", "start" => "11:00" }, > { "end" => "11:30" }, > { "end" => "12:30", "id" => "1" }, > { "id" => "2", "name" => "mow lawn" }, > { "name" => "buy milk" }, > { "name" => "clean room" }] }} > > > If I ommit the :index => :nil, I get an error "conflicting types for > parameter containers. Expected an instance of Array but found an > instance of Hash" called on task_attribues. What I need is exactly > that > - a hash grouping each task by id with it''s attributes: > { "task_attributes" => > { "1" => { "name" => ''buy milk'', > "start" => ''10:00'', > "end" => ''10:30'' } > "2" => {..}...} >I''m guessing the problem here is new records ? the easiest way out of here is to drop all the :index => nil, but if the record is new the call fields_for ..., :index => ''something'' new records would then appear with that key in the task_attributes hash (obviously make the index unique) Fred> I''m feeling like I''m missing something big here... Any help will be > great - thanks. > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On 23 Feb 2009, at 15:57, sa 125 wrote: >> { "id" => "2", "name" => "mow lawn" }, >> { "1" => { "name" => ''buy milk'', >> "start" => ''10:00'', >> "end" => ''10:30'' } >> "2" => {..}...} >> > > I''m guessing the problem here is new records ? the easiest way out of > here is to drop all the :index => nil, but if the record is new the > call fields_for ..., :index => ''something'' > > new records would then appear with that key in the task_attributes > hash (obviously make the index unique) > > FredI did as you proposed and seperated the new record from the edited one. I get the hash I wanted indexed by id (for edit), but apparently rails doesn''t like it and throws an error: Conflicting types for parameter containers. Expected an instance of Array but found an instance of Hash.. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I read that rails 2.3 will handle nested models seamlessly with accepts_nested_attributes_for inside the parent model (http://guides.rubyonrails.org/2_3_release_notes.html). Any idea when the final 2.3 is going to be released (currently in RC1)? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---