Note that there are some explanatory texts on larger screens.

plurals
  1. POHABTM Form not working in rails. Not submiting into Join tables
    primarykey
    data
    text
    <p>I am trying to assign a team to a player. A player can have many teams but upon initial set up they only have one. I am trying to set up a choice so the user can pick a team.</p> <p>When I submit the form the data for the other fields, first_name and last_name submit fine however no data gets saved in the join table. I am using a select box however check boxes will do the job too if someone can think of that.</p> <h2>Models</h2> <p>I have 2 models. Players and Teams set up with a HABTM relationship.</p> <pre><code>class Player &lt; ActiveRecord::Base has_and_belongs_to_many :teams class Team&lt; ActiveRecord::Base has_and_belongs_to_many :players </code></pre> <h2>Controller</h2> <pre><code> def new @player = Player.new end def create @player = Player.new(params[:player]) if @player.save flash[:notice] = "Player Created" redirect_to(:action =&gt;'list') else render('new') end end </code></pre> <h2>Join table</h2> <p>I can insert data into the the join table from the rails console. I can then get this data and show it. So the relationships work.</p> <pre><code>create_table "players_teams", :id =&gt; false, :force =&gt; true do |t| t.integer "player_id" t.integer "team_id" end add_index "players_teams", ["player_id", "team_id"], :name =&gt; "index_players_teams_on_player_id_and_team_id" </code></pre> <h2>View</h2> <p>In my view I have this </p> <pre><code>&lt;%= f.collection_select(:team_id, Team.all, :id, :name, :prompt =&gt; true) %&gt; </code></pre> <p>This loads a view with the teams Populated. Once submitted I get</p> <pre><code>Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"kvOmx3G5H1mqLMnEn6HS3a79+WQnIzfsUA3Dt0XHo1w=", "player"=&gt;{"first_name"=&gt;"Test", "last_name"=&gt;"Data", "email"=&gt;"email@email.com"}, "teams"=&gt;{"team_id"=&gt;"1"}, "commit"=&gt;"create player"} </code></pre> <p>I am not sure where to go from here. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload