我有一个模型 Post,它与另一个模型 Tag 有多对多的关系:
class Post < ActiveRecord::Base
attr_accessible :body, :section_id, :solved, :subject, :tag_ids, :user_id
belongs_to :user
belongs_to :section
has_and_belongs_to_many :tags
has_many :comments
这是标签:
class Tag < ActiveRecord::Base
attr_accessible :subject, :post_ids
has_and_belongs_to_many :posts
validates(:subject, presence: true, :length => { minimum: 3, maximum: 15})
end
创建帖子时,我需要允许用户在创建帖子时以与帖子标题和正文相同的形式添加标签。标签是预先创建的,永远不会在帖子中创建。我不知道该怎么做。我想使用 form_for 以便可以轻松地填充 Post 的字段,但我不知道如何将标签合并到其中并将其发送到控制器。有什么建议么?这是我的控制器发送到视图的内容:
@post = Post.new
@tags = Tag.all