HABTM表单不在rails中提交多个值

时间:2012-03-27 20:24:06

标签: ruby-on-rails has-and-belongs-to-many form-for

我正试图通过表格将一组玩家分配给一个夹具。

我一直试图通过复选框从我的球员表中列出所有球员,这样用户就可以选择他想要的球员。

模型 有三个模型与灯具和球员保持

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

我需要使用:through作为球员和赛程的关联。我需要一个布尔值,不管那个人是否已付款。

这些工作的关联,我可以通过控制台将玩家分配到连接表中的夹具。

视图 在我看来,我有:

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

这是我此刻试图开始工作的代码!

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

修改

我现在已经插入了以下评论,并且还将集合选择更改为:

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

目前,我在尝试加载表单时遇到以下错误:

undefined method `merge' for :name:Symbol

在这一行

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

这可能是非常错误的,但是我真的需要让它工作,因为这是我项目的很大一部分。

有人能指出我正确的方向吗?如果有人想沿着这条路线发送,请随意,因为我也无法做到这一点。

1 个答案:

答案 0 :(得分:0)

您应该将对象传递给form_for而不是符号:

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

应该是

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

<强>更新

看起来你没有在Fixture模型中定义has_many:player_fixtures,has_many:首先需要它。

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