Hi, I am a bit of a rails novice and trying to work this out. Been tearing my hair out over this for a long time. I have 2 models that I am working on at the moment. The idea is that Teams have many players and Players have many teams. I want to be able to assign a starting team to a player when they are created. class Team < ActiveRecord::Base has_and_belongs_to_many :players class Player < ActiveRecord::Base has_and_belongs_to_many :teams *Note: I have set up the correct join table and can insert these relationships from the ruby console. * At the moment I have this in my view: <%= select(:player_id, :teams, Team.all.collect{|s| [s.name, s.id]}) %> It collects all the team names however when I hit submit nothing is sent. Am I missing out something? Thank you. I have spent ages trying to sort this out. -- 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-/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.
On 29 February 2012 16:38, JKnegtel <jpknegtel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, I am a bit of a rails novice and trying to work this out. Been > tearing my hair out over this for a long time. > > I have 2 models that I am working on at the moment. The idea is that > Teams have many players and Players have many teams. I want to be able > to assign a starting team to a player when they are created. > > class Team < ActiveRecord::Base > has_and_belongs_to_many :players > > class Player < ActiveRecord::Base > has_and_belongs_to_many :teams > > *Note: I have set up the correct join table and can insert these > relationships from the ruby console. * > > At the moment I have this in my view: > > <%= select(:player_id, :teams, Team.all.collect{|s| [s.name, s.id]}) > %> > > It collects all the team names however when I hit submit nothing is > sent. Am I missing out something?Do you mean f.select( :player ...) rather than just select? Assuming you are inside a form_for that is. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, thanks for your reply. I am indeed that a form_for. When I put an f. infrount i get an error. undefined method `merge'' for [["1st Team", 1], ["2nd Team", 2]]:Array Just to confirm. Without the f. the form loads however no Team data gets sent to the Params on the create. Sorry that is a bit vauge and probably the wrong way of saying it!>-- 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/-/lreqcLm2FukJ. 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.
I have made some progress.> > <%= select(:player, :team, Team.all.collect{|s| [s.name, s.id]}) %>However I now get an error when I submit the form. One that is gettting closer!> > undefined method `each'' for "2":StringAND THIS IS THE PARAMETERS "player"=>{"first_name"=>"test", "last_name"=>"2", "email"=>"eamiltest-J0of1frlU80@public.gmane.org", "teams"=>"2"}, "commit"=>"create player"}>-- 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/-/H5ugFaDYcIEJ. 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.
<%= collection_select(:player, :team, Team.all, :id, :name, :prompt => true) %> i think it will work for u.assume you have form_for On Fri, Mar 2, 2012 at 2:10 AM, JKnegtel <jpknegtel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have made some progress. >> >> <%= select(:player, :team, Team.all.collect{|s| [s.name, s.id]}) %> > > However I now get an error when I submit the form. One that is gettting > closer! >> >> undefined method `each'' for "2":String > > AND THIS IS THE PARAMETERS > > "player"=>{"first_name"=>"test", > "last_name"=>"2", > "email"=>"eamiltest-J0of1frlU80@public.gmane.org", > "teams"=>"2"}, > "commit"=>"create player"} > > >> -- > 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/-/H5ugFaDYcIEJ. > > 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. >-- 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-/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.
O had to put :teams not :team but this works, However when I hit create it comes back with a new error undefined method `each'' for "2":String On Sunday, 4 March 2012 09:52:56 UTC, vishal wrote:> > <%= collection_select(:player, :team, Team.all, :id, :name, :prompt => true) %> > > i think it will work for u.assume you have form_for > > >-- 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/-/BiGjGTP2ttsJ. 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.
ok u just write <%= f.collection_select(:player, Team.all, :id, :name, : prompt => true) %> it will store team id in ur player attrbutes On Sun, Mar 4, 2012 at 4:36 PM, JKnegtel <jpknegtel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> O had to put :teams not :team but this works, > > However when I hit create it comes back with a new error undefined method > `each'' for "2":String > > On Sunday, 4 March 2012 09:52:56 UTC, vishal wrote: >> >> <%= collection_select(:player, :team, Team.all, :id, :name, :prompt => true) %> >> >> i think it will work for u.assume you have form_for >> >> >> -- > 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/-/BiGjGTP2ttsJ. > > 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. >-- 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-/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.
This does not work. Using your code from before: <%= collection_select(:players, :teams, Team.all, :id, :name, :prompt => true) %> The form now submits however: *NO DATA IS SAVED IN THE JOIN TABLE IN THE HABTM RELATIONSHIP. * -- 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/-/OvuhoTIhwNUJ. 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.
On 4 March 2012 20:42, JKnegtel <jpknegtel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This does not work. > > Using your code from before: <%= collection_select(:players, :teams, > Team.all, :id, :name, :prompt => true) %> > The form now submits however: > > NO DATA IS SAVED IN THE JOIN TABLE IN THE HABTM RELATIONSHIP.WELL YOU HAVE SOMETHING WRONG IN YOUR CONTROLLER CODE THEN. Have look at the Rails Guide on debugging to find out how to debug the code. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.