Rails 3:通过关联使用 has_many 进行多项选择 [英] Rails 3: Multiple Select with has_many through associations

查看:29
本文介绍了Rails 3:通过关联使用 has_many 进行多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望可以通过多项选择为一个帖子选择多个类别.

I want to get possibility to select several Categories for one Post with multiple select.

我有下一个模型:Post、Category 和 PostCategory.

I have next models: Post, Category and PostCategory.

class Post < ActiveRecord::Base
  has_many :post_categories
  has_many :categories, :through => :post_categories
end

class Category < ActiveRecord::Base
  has_many :post_categories
  has_many :posts, :through => :post_categories
end

class PostCategory < ActiveRecord::Base
  has_one    :post
  has_one    :category
  belongs_to :post      # foreign key - post_id
  belongs_to :category  # foreign key - category_id
end

在我的控制器中,我有类似 @post = Post.new 的东西.我创建了一些类别.

In my controller I have something like @post = Post.new . I've created some categories.

我认为:

<%= form_for @post do |f| %>
    <%= f.text_field :title %>
    <%= f.select :categories, :multiple => true %>
    <%= f.submit %>
<% end %>

而且...我的类别在哪里?我在选择选项中只有多个".我觉得我的表格有问题.

And... where is my categories? I have only "multiple" in select options. I think it's something wrong with my form.

推荐答案

很抱歉让死者复活,但我找到了一个更简单的解决方案,它允许使用默认控制器操作代码并使用 ActiveModel setter 逻辑来处理 has_many.是的,这完全是魔术.

Sorry to resurrect the dead, but I found a much simpler solution that lets one use the default controller action code and use the ActiveModel setter logic for a has_many. Yes, it's totally magic.

<%= f.select :category_ids, Category.all.collect {|x| [x.name, x.id]}, {}, :multiple => true %>

特别是,使用 :category_ids(或 :your_collection_ids)参数名称会自动告诉 Rails 调用@post.category_ids = params[:post][:category_ids] 相应地设置该帖子的类别,无需修改默认的控制器/支架#create 和#update 代码.

Specifically, using the :category_ids (or :your_collection_ids) param name will automagically tell Rails to call @post.category_ids = params[:post][:category_ids] to set the categories for that post accordingly, all without modifying the default controller/scaffold #create and #update code.

哦,它与 has_many :something 一起工作,通过::something_else 自动管理连接模型.太棒了.

Oh, and it works with has_many :something, through: :something_else automatically managing the join model. Freaking awesome.

因此,在 OP 中,只需将字段/参数名称更改为 :category_ids 而不是 :categories.

So from the OP, just change the field/param name to :category_ids instead of :categories.

这也将自动让模型的选定类别填充在编辑表单上突出显示的选择字段.

This will also automatically have the model's selected categories populate the select field as highlighted when on an edit form.

参考文献:

来自 has_many API 文档我在哪里找到的.

还有 来自表单助手指南的警告 在不使用正确的表单字段/参数名称时解释了这种类型不匹配".

Also the warning from the form helpers guide explains this "type mismatch" when not using the proper form-field/parameter name.

通过使用正确的表单字段/参数名称,您可以像 Rails 方式所鼓励的那样,干掉新的和编辑的表单并保持控制器的精简.

By using the proper form-field/param name, you can dry up new and edit forms and keep the controllers thin, as encouraged by the Rails way.

rails 4 和强参数的注意事项:

def post_params
  params.require(:post).permit(:title, :body, category_ids: [])
end

这篇关于Rails 3:通过关联使用 has_many 进行多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆