从另一个型号下拉菜单 [英] Drop down menu with value from another model

查看:94
本文介绍了从另一个型号下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有产品属于系列。一个集合只是一个名字。产品有一个collection_id。



在我的_form视图中,用于创建和版本的产品,我想要一个带有所有集合名称的下拉菜单。



问题,似乎没有关联form.for的选择方法,我正在尝试使用:


select(method,choices,options = {},
html_options = {})


从文档,但我不明白。我必须写一个方法来创建一个表单?有什么选择和2个选项?两个参数应该足以填充<选项>
标签。



如何使用下拉式选单让我通过收藏名称将产品分配给我的产品?

解决方案

您可以使用集合选择,首先确保您的模型正确设置:

  class Product 
belongs_to:collection
end

class Collection
has_many:products
end

然后将集合选择添加到您的视图:

 <%= collection_select(:product,:collection_id,Collection.all,:id,:name)%> 

您还可以阅读文档 here


I have products belonging to collections. A collection is just a name. Products have a collection_id.

In my _form view that is used for creation and edition of products, i'd like to have a drop down menu with the name of all collection.

Problem, it seems there is no select method affiliated to form.for and i am trying to use :

select(method, choices, options = {}, html_options = {})

from the doc but i do not understand it. I must write a methode to create a form? What are the choices, and the 2 options? Two parameters should be enough to populate an < option> tag.

How can I have a drop down menu alowing me to assign a collection through collection name to my product?

解决方案

You can use a collection select, first make sure your models are properly setup:

class Product
  belongs_to :collection
end

class Collection
  has_many :products
end

Then add the collection select to your view:

<%= collection_select(:product, :collection_id, Collection.all, :id, :name) %>

You can also read up on the documentation here.

这篇关于从另一个型号下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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