Ruby on Rails的f.select与自定义属性选项 [英] ruby on rails f.select options with custom attributes

查看:950
本文介绍了Ruby on Rails的f.select与自定义属性选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格select语句,像这样的:

  = f.select:COUNTRY_ID,@ countries.map {| C | [c.name,c.id]}

这将导致该code:

  ...
<期权价值=1>安道尔< /选项>
<期权价值=2>阿根廷< /选项>
...

但我想自定义的HTML属性添加到我的选择,像这样的:

  ...
<期权价值=1currecy_ code =XXX>安道尔< /选项>
<期权价值=2currecy_ code =YYY>阿根廷< /选项>
...


解决方案

Rails可以添加自定义属性来选择选项,使用现有的options_for_select帮手。你几乎有它的权利在你的问题中code。使用HTML5数据属性:

 <%= f.select:COUNTRY_ID,options_for_select(@ countries.map {| C | [c.name,c.id,{'数据currency_ code '=> c.currency_ code}]})%GT;

添加一个初始的选择:

 <%= f.select:COUNTRY_ID,options_for_select(@ countries.map {| C | [c.name,c.id,{'数据currency_ code '=> c.currency_ code}]},SELECTED_KEY = f.object.country_id)%GT;

如果您需要分组的选择,你可以使用grouped_options_for_select帮手,像这样的(如果@continents是,每一个都具有国家法的大陆对象的数组):

 <%= f.select:COUNTRY_ID,grouped_options_for_select(@ continents.map {|组| [group.name,group.countries.map {| C | [c.name ,c.id,{'数据currency_ code'=> c.currency_ code}]}]},SELECTED_KEY = f.object.country_id)%GT;

信贷应该去保罗@ pogodan谁张贴了关于在文档中没有找到这一点,但通过阅读导轨来源。 <一href=\"https://web.archive.org/web/20130128223827/http://www.pogodan.com/blog/2011/02/24/custom-html-attributes-in-options-for-select\">https://web.archive.org/web/20130128223827/http://www.pogodan.com/blog/2011/02/24/custom-html-attributes-in-options-for-select

I have a form select statement, like this:

= f.select :country_id, @countries.map{ |c| [c.name, c.id] }

Which results in this code:

...
<option value="1">Andorra</option>
<option value="2">Argentina</option>
...

But I want to add a custom HTML attribute to my options, like this:

...
<option value="1" currecy_code="XXX">Andorra</option>
<option value="2" currecy_code="YYY">Argentina</option>
...

解决方案

Rails CAN add custom attributes to select options, using the existing options_for_select helper. You almost had it right in the code in your question. Using html5 data-attributes:

<%= f.select :country_id, options_for_select(@countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] }) %>

Adding an initial selection:

<%= f.select :country_id, options_for_select(@countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] }, selected_key = f.object.country_id) %>

If you need grouped options, you can use the grouped_options_for_select helper, like this (if @continents is an array of continent objects, each having a countries method):

<%= f.select :country_id, grouped_options_for_select(@continents.map{ |group| [group.name, group.countries.map{ |c| [c.name, c.id, {'data-currency_code'=>c.currency_code}] } ] }, selected_key = f.object.country_id) %>

Credit should go to paul @ pogodan who posted about finding this not in the docs, but by reading the rails source. https://web.archive.org/web/20130128223827/http://www.pogodan.com/blog/2011/02/24/custom-html-attributes-in-options-for-select

这篇关于Ruby on Rails的f.select与自定义属性选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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