在Rails下拉列表中,如何显示模型中的所有列名称? [英] In Rails dropdown, how to display all column names from a model?

查看:127
本文介绍了在Rails下拉列表中,如何显示模型中的所有列名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails下拉菜单,如下所示:

I have a Rails dropdown as shown below:

<%= select_tag(:category, options_for_select([['Model1', 0], ['Model2', 1], ['Model3', 2], ['Model4', 3]], :selected => 0), :multiple => true) %>

默认情况下将选择 Model1 以上。上面下拉列表中的每个项目都是Rails模型(显然有其自己的数据库表)

By default Model1 would be selected as shown above. Each item in the dropdown above is a Rails model (which obviously has its own database table)


  1. 现在,我想显示另一个下拉列表它将显示Rails模型 Model1 的表中的所有列名称。

  2. 如果用户在上面的下拉列表中将选择更改为 Model2 ,则第二个下拉列表应显示 Model2

  3. 如果用户从上面选择了多个选项,那么我想显示用户已选择的所有模型中的列。

  1. Now, I would like to display another dropdown which would display all the column names from the Rails model Model1's table.
  2. If the user changes the selection to Model2 in the dropdown above, then the second dropdown should display all the column names from Model2.
  3. If the user selects multiple options from the above, then I would like to display columns from all the models which the user had selected.

请问如何实现?

推荐答案

如上所述, ActiveRecord#column_names 是来填充您的第二个下拉列表。

As mentioned above, ActiveRecord#column_names is the way to populate your second dropdown.

现在,如果您想要的是两个下拉列表之间的动态关系-第二个下拉列表将根据第一个下拉列表填充信息-仅Rails会还不够。

Now, if what you want is a dynamic relation between two dropdowns - the second being filled with information depending on the first one - Rails alone will not be enough.

您需要添加一些JavaScript才能实现该目标,我建议您阅读由@rlarcombe回答

You will need to add a bit of javascript to achieve that goal and I suggest you read that answer by @rlarcombe on the matter.

这篇关于在Rails下拉列表中,如何显示模型中的所有列名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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