[Rails] HABTM forms problem. Select form tag

212 views
Skip to first unread message

JKnegtel

unread,
Feb 29, 2012, 11:38:18 AM2/29/12
to Ruby on Rails: Talk
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.



Colin Law

unread,
Mar 1, 2012, 8:24:25 AM3/1/12
to rubyonra...@googlegroups.com

Do you mean f.select( :player ...) rather than just select? Assuming
you are inside a form_for that is.

Colin

JKnegtel

unread,
Mar 1, 2012, 3:02:27 PM3/1/12
to rubyonra...@googlegroups.com
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!

JKnegtel

unread,
Mar 1, 2012, 3:40:49 PM3/1/12
to rubyonra...@googlegroups.com
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"=>"eami...@test.com",
 "teams"=>"2"},
 "commit"=>"create player"}

vishal singh

unread,
Mar 4, 2012, 4:52:56 AM3/4/12
to rubyonra...@googlegroups.com
<%= 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/-/H5ugFaDYcIEJ.

To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

JKnegtel

unread,
Mar 4, 2012, 6:06:38 AM3/4/12
to rubyonra...@googlegroups.com
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

vishal singh

unread,
Mar 4, 2012, 12:04:58 PM3/4/12
to rubyonra...@googlegroups.com
ok u just write <%= f.collection_select(:player, Team.all, :id, :name, :prompt => true) %> it will store team id in ur player attrbutes

--
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.

JKnegtel

unread,
Mar 4, 2012, 3:42:08 PM3/4/12
to rubyonra...@googlegroups.com
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. 

Colin Law

unread,
Mar 4, 2012, 4:47:25 PM3/4/12
to rubyonra...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages