HABTM forms problem. Select form tag

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.

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!

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”=>“[email protected]”,
“teams”=>“2”},
“commit”=>“create player”}

On 29 February 2012 16:38, JKnegtel [email protected] wrote:

class Player < ActiveRecord::Base
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

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

ok u just write <%= f.collection_select(:player, Team.all, :id, :name, :
prompt => true) %> it will store team id in ur player attrbutes

<%= collection_select(:player, :team, Team.all, :id, :name, :prompt =>
true) %>

i think it will work for u.assume you have form_for

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

On 4 March 2012 20:42, JKnegtel [email protected] 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs