Ruby on Rails form_for 选择带有类的字段 [英] Ruby on Rails form_for select field with class

查看:26
本文介绍了Ruby on Rails form_for 选择带有类的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用头撞墙.我想使用 f.select 标签制作一个简单的选择标签,但我所做的一切都不起作用.我在下面举了一个例子:

I am beating my head against the wall on this one. I want to make a simple select tag using the f.select tag but nothing I do works. I put an example below:

<%= f.select(:object_field, ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 4'], :class => 'my_style_class')%>

好的,基本上它是一个简单的列表,一旦提交表单,它就会将值放入object_field.一切正常,但查看页面源时不包含 class 标记.它不会抛出错误,只是一起跳过.

Ok, so basically it is a simple list that once the form is submitted it places the value into the object_field. That all works, but viewing the page source the class tag is not included. It doesn't throw an error, it just skips it all together.

如果有人有任何建议,我将不胜感激.

If anyone has any suggestions I would greatly appreciate it.

推荐答案

试试这个:

<%= f.select(:object_field, ['Item 1', ...], {}, { :class => 'my_style_class' }) %>

select helper 接受两个选项哈希,一个用于选择,第二个用于 html 选项.因此,您只需在项目列表之后将默认的空选项作为第一个参数,然后将您的类添加到 html_options.

select helper takes two options hashes, one for select, and the second for html options. So all you need is to give default empty options as first param after list of items and then add your class to html_options.

http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

这篇关于Ruby on Rails form_for 选择带有类的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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