0

我正在使用http://www.youtube.com/watch?v=PUHmX18Zmyk视频制作博客。我的帖子模型是

class Post < ActiveRecord::Base
  attr_accessible :body, :publish_date, :title, :user_id
  validates_presence_of :title, :body, :publish_date, :user_id
  belongs_to :user
  has_and_belongs_to_many :categories
end

类别模型是

class Category < ActiveRecord::Base
  attr_accessible :name
  validates_presence_of :name
  has_and_belongs_to_many :posts
end

在视频中,他使用此类代码在新帖子中生成类别列表

<%  f.association :categories, :as => :checkboxes %>

但我有错误

undefined method `association' for #<ActionView::Helpers::FormBuilder:0xb5e82b40>

制作类别列表的正确方法是什么?

4

1 回答 1

0

您应该查看 rails 文档以制作一个选择框,这将允许您选择类别名称,但将 category_id 与帖子一起发送。

http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

于 2012-08-27T22:40:25.233 回答