Rails 中的下拉框 [英] Drop down box in Rails
本文介绍了Rails 中的下拉框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 Rails 创建下拉选择框?说我是否已经完成了查询:
How do I use Rails to create a drop-down selection box? Say if I have done the query:
@roles = Role.all
那么如何显示一个包含所有@roles.name 的框?
Then how do I display a box with all the @roles.name's ?
在实现下拉框之后.我如何让它响应选择?我应该制作表格吗?
After implementing the dropdown box. How do I make it respond to selections? Should I make a form?
推荐答案
使用 collection_select 帮助器http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001593
use the collection_select helper http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#M001593
这将允许您编写如下内容:
This will allow you to write something like:
collection_select(:user, :role_id, @roles, :id, :role_title, {:prompt => true})
并得到
<select name="user[role_id]">
<option value="">Please select</option>
<option value="1" selected="selected">Administrator</option>
<option value="2">User</option>
<option value="3">Editor</option>
</select>
这篇关于Rails 中的下拉框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文