如何从 Rails 的下拉列表中获取选定的文本
我的选项在一个数组中
@groupings = [["Text 1", "value_1"],["Text 2", "value_2"]]
,所以我使用
select_tag(:groupby, options_for_select(@groupings, params[:groupby]))
我想用所选文本填充页面上的 H3 标签。 (即“Text 2”,如果 params[:groupby ] = "value_2" )
有没有一种 Rails 方法可以做到这一点?除了从 @groupings 数组手动派生它之外?
TIA, 卡比
I have my options in an array
@groupings = [["Text 1", "value_1"],["Text 2", "value_2"]]
so im using a
select_tag(:groupby, options_for_select(@groupings, params[:groupby]))
I'd like to populate an H3 tag on the page with the selected text. (ie "Text 2" if params[:groupby ] = "value_2" )
Is there a rails way to do this? Other than manually deriving it from the @groupings array?
TIA,
Kirby
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您所需要的只是一个简单的
find
调用,并且这些通常最好包装在助手中:然后:
如果您有
@groupings 的哈希版本,则此查找会更容易
变量,或者可能只是将其保留为哈希并在需要时使用.to_a
将其呈现为数组。Maybe all you need is a simple
find
call, and these are often best wrapped in a helper:Then later:
This look-up would be easier if you had a Hash version of your
@groupings
variable, or perhaps simply kept it as a Hash and rendered it as an Array using.to_a
where required.