如何在Rails中应用HTML选项选择具有单维数组的表单助手

时间:2017-02-02 05:35:31

标签: html css ruby-on-rails forms

我尝试按照Rails文档介绍如何使用select form helper,并提出了这个:

<%= f.select :datatype, options_for_select(%w(string select text)), prompt: 'Select a datatype', class: 'chosen-select' %>

这适用于现在的错误,但HTML选项(在本例中为类规范)不会被应用。我也尝试过使用html: {class: 'chosen-select'},这也没有错误(但也没有用)。

这会产生:

<select name="some_object[datatype]" id="some_object_datatype">
<option value="">Select a datatype</option>
<option value="string">string</option>
<option value="select">select</option>
<option value="text">text</option></select>

我做错了什么?

1 个答案:

答案 0 :(得分:3)

class被视为options参数的一部分:

select(object, method, choices = nil, options = {}, html_options = {}

将提示括在大括号中修复:

<%= f.select :datatype, options_for_select(%w(string select text)), {prompt: 'Select a datatype'}, {class: 'chosen-select'} %>