简体   繁体   中英

HABTM Form not submitting more than one value in rails

I am trying to assign a group of players to a fixture though a form.

I have been trying to list all the players from my players table via checkboxes so the user can select which players he wants for the fixture.

Models There are 3 models concedered with the fixtures and players

class Fixture < ActiveRecord::Base
belongs_to :team
has_many :player_fixtures
has_many :players, :through => :player_fixtures
has_one :venue

class Player < ActiveRecord::Base
has_and_belongs_to_many :teams
has_many :player_fixtures
has_many :fixtures, :through => :player_fixtures

class PlayerFixture < ActiveRecord::Base
belongs_to :player
belongs_to :fixture

I need to use a :through as with the association of the players and fixtures. I need a boolean whether that person has paid.

The associations of these work, and I can assign a player to a fixture in the join table though the console.

Views In my view I have:

 <%= form_for(:fixture, :url => {:action =>'create'}) do |f| %>

This is the code I have been trying to get to work at the moment!

 <%= f.collection_select(:player, :player_id, @players,:id, :name, { :include_blank => true ,:multiple => true}) %>

EDIT

I have now inserted the comment below and also changed the collection select to:

 <%= f.collection_select(:player_ids, Player.all,:id, :name, {:include_blank => true ,:multiple => true}) %> 

At the moment I get the following error on trying to load the form:

undefined method `merge' for :name:Symbol

on this line

 28:       <td><%= f.collection_select(:fixture, :player_id, Player.all,:id, :name, { :include_blank => true ,:multiple => true}) %>

This is probably horribly wrong, however I really need to get this working as this is a large part of my project.

Could anyone point me in the right direction? If someone wants to send along the formtastic route, feel free, as I can not get that work either.

You should pass object to the form_for instead of symbol:

<%=form_for(:fixture, :url => {:action =>'create'}) do |f| %>

should be

<%= form_for(@fixture, :url => {:action =>'create'}) do |f| %>

Update:

Look like you did not define has_many :player_fixtures in the Fixture model, has_many :through required it first.

class Fixture < ActiveRecord::Base
belongs_to :team
has_many :player_fixtures
has_many :players, :through => :player_fixtures
has_one :venue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM