Ruby Rails:通过HABTM关系的复选框的表单分配多个参数

Ruby Rails:通过HABTM关系的复选框的表单分配多个参数,ruby,ruby-on-rails-3,checkbox,has-and-belongs-to-many,Ruby,Ruby On Rails 3,Checkbox,Has And Belongs To Many,我是一个rails noob,正在做我自己的第一个项目 我得到了两个模型,它们用一个has_和(你属于)的关系连接起来:葡萄酒和商店。简单地说,一种葡萄酒可以在不同的商店出售,一家特定的商店可以出售许多不同的葡萄酒。以下是模型: class Shop < ActiveRecord::Base has_and_belongs_to_many :wines end class Wine < ActiveRecord::Base has_and_belongs_to_many :s

我是一个rails noob,正在做我自己的第一个项目

我得到了两个模型,它们用一个has_和(你属于)的关系连接起来:葡萄酒和商店。简单地说,一种葡萄酒可以在不同的商店出售,一家特定的商店可以出售许多不同的葡萄酒。以下是模型:

class Shop < ActiveRecord::Base
 has_and_belongs_to_many :wines
end

class Wine < ActiveRecord::Base
  has_and_belongs_to_many :shops
end
只要让我知道,如果你需要任何其他细节来处理这个问题,或者如果已经有一个关于这个问题,我错过了

提前感谢

试试这个

<%  @shops.each do |t|  %>
  <%= f.label t.name  %>
  <%= check_box_tag "shops[]", t.id %>
<% end %>

您还可以简化crreate方法:`@wine=wine.new(params[:wine])@wine.shops对于3:Fixnum`我仍然得到
未定义的方法
merge',其中3是其中一个shop@Santosh已经试过了;)我得到了#@BroiSatse的
未定义方法
复选框标记谢谢,但它似乎并不能解决视图的问题。@Santosh-您将得到葡萄酒的“未知属性”商店。您需要将@shops assignment移到@wine assignment上,并将其更改为
@shops=Shop.find params[:wine]。删除(:shops)
<%  @shops.each do |t|  %>
<%= f.label t.name  %>
<%= f.check_box :shops, t.id %>
<% end %>
undefined method `merge' for 3:Fixnum
<%  @shops.each do |t|  %>
  <%= f.label t.name  %>
  <%= check_box_tag "shops[]", t.id %>
<% end %>
def create
  @wine = Wine.new(params[:wine])
  @shops = Shop.find params[:shops]
  @wine.shops = @shops
  ..