Say I have a Dog that has_may legs (In this case we don''t care how many). In the _form for Dog, I would like to assign a pre-existing let to him. I would like to do something like this: <%= f.collection_select :leg_1, Leg.select { |l| l.dog_id == nil }, :id, :description %> <%= f.collection_select :leg_2, Leg.select { |l| l.dog_id == nil }, :id, :description %> Then in my controller''s create action: @dog = Dog.new(params[:dog]) @dog.save leg = Leg.find(params[:leg_1]) leg.dog = @dog leg.save The problem is that the collection_select is trying to set the leg_1 value as part of Dog, which of course does not exist. Do I need to create ''view model'' for doing this? Thanks, ~S -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/L0hQFvIaEFgJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
Could this be done with fields_for? On Friday, 29 June 2012 15:02:52 UTC-6, sheamus wrote:> > Say I have a Dog that has_may legs (In this case we don''t care how many). > In the _form for Dog, I would like to assign a pre-existing let to him. I > would like to do something like this: > > <%= f.collection_select :leg_1, Leg.select { |l| l.dog_id == nil }, :id, > :description %> > <%= f.collection_select :leg_2, Leg.select { |l| l.dog_id == nil }, :id, > :description %> > > Then in my controller''s create action: > > @dog = Dog.new(params[:dog]) > @dog.save > leg = Leg.find(params[:leg_1]) > leg.dog = @dog > leg.save > > The problem is that the collection_select is trying to set the leg_1 value > as part of Dog, which of course does not exist. Do I need to create ''view > model'' for doing this? > > Thanks, > ~S > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/FJhqhsHfyWYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
While I believe it is possible to use fields_for, as you mentioned in your replies, you do not have to. Here is a possible solution: # app/controllers/dogs_controller.rb class DogsController < ApplicationController def new @dog = Dog.new @legs = Leg.where(:dog_id => nil) end def create @dog = Dog.new(params[:dog]) @dog.save end end # app/views/dogs/new.html.erb <%= form_for @dog do |f| %> <%= f.label :name %>: <%= f.text_field :name %><br /> <% 4.times do |i| %> <%= f.label "leg_#{i}", "Leg #{i+1}" %>: <%= select_tag ''dog[leg_ids][]'', options_from_collection_for_select(@legs, ''id'', ''description''), :id => "dog_leg_#{i}" %><br /> <% end %> <%= f.submit %> <% end %> Note that you will have issues if you choose the same leg for any of the selects. I hope this helps. -- Andrew Ferk On Friday, June 29, 2012 4:02:52 PM UTC-5, sheamus wrote:> > Say I have a Dog that has_may legs (In this case we don''t care how many). > In the _form for Dog, I would like to assign a pre-existing let to him. I > would like to do something like this: > > <%= f.collection_select :leg_1, Leg.select { |l| l.dog_id == nil }, :id, > :description %> > <%= f.collection_select :leg_2, Leg.select { |l| l.dog_id == nil }, :id, > :description %> > > Then in my controller''s create action: > > @dog = Dog.new(params[:dog]) > @dog.save > leg = Leg.find(params[:leg_1]) > leg.dog = @dog > leg.save > > The problem is that the collection_select is trying to set the leg_1 value > as part of Dog, which of course does not exist. Do I need to create ''view > model'' for doing this? > > Thanks, > ~S > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/-rfMoppdocIJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
Thank. Yeah, I basically ended up doing something similar. I use ''leg[]'' in my select tag, and then manually hooked things up in the back end.... I wonder if in your solution if things would have been connected for me? Never mind just tried it, I still have to hook things up manually in the backend. Thanks for the help, good to know that I am not crazy. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/jKbJiF-Gb98J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
S, You should not have to do any manual association. If you have params[:dog][:leg_ids] = [...], it should work; you may need to set attr_accessible :leg_ids in your Dog model. Show your solution, and maybe we can refactor it to use less code. -- Andrew Ferk On Sunday, July 1, 2012 10:39:14 AM UTC-5, sheamus wrote:> > Thank. Yeah, I basically ended up doing something similar. I use ''leg[]'' > in my select tag, and then manually hooked things up in the back end.... I > wonder if in your solution if things would have been connected for me? > Never mind just tried it, I still have to hook things up manually in the > backend. > > Thanks for the help, good to know that I am not crazy. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ivYnm3vaIHcJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
S, You should not have to do any manual association. If you have params[:dog][:leg_ids] = [...], it should work; you may need to set attr_accessible :leg_ids in your Dog model. Show your solution, and maybe we can refactor it to use less code. -- Andrew Ferk On Sunday, July 1, 2012 10:39:14 AM UTC-5, sheamus wrote:> > Thank. Yeah, I basically ended up doing something similar. I use ''leg[]'' > in my select tag, and then manually hooked things up in the back end.... I > wonder if in your solution if things would have been connected for me? > Never mind just tried it, I still have to hook things up manually in the > backend. > > Thanks for the help, good to know that I am not crazy. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/nxLK3MZWKw4J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
Ahhh, got it working as you said now. Great thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/RJvnPzGPAqAJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.