Rails表格选择要求
问题描述:
我想要在我的Rails表格中使用<select>
。Rails表格选择要求
这是我的代码(省略号是使线较短):
<div class="field">
<p><%= f.label :category, "Category:" %></p>
<%= f.select :category, ['Analytics','Commerce',..., 'Web'], :prompt => '-- Select One --', :required => true %>
</div>
,输出
<div class="field">
<p><label for="startup_category">Category:</label></p>
<select id="startup_category" name="startup[category]">
<option value="">-- Select One --</option>
<option value="Analytics">Analytics</option>
<option value="Commerce">Commerce</option>
<option value="Content Management">Content Management</option>
<option value="Gaming">Gaming</option>
<option value="Green">Green</option>
<option value="Media">Media</option>
<option value="Social Media">Social Media</option>
<option value="Technology - Software">Technology - Software</option>
<option value="Technology - Hardware">Technology - Hardware</option>
<option value="Web">Web</option></select>
</div>
把{:required => true}
而不是:required => true
给出了一个语法错误和{:prompt => '-- Select One --', :required => true}
呈现页面,但没有我的选择标签中的required="true"
。
如何在我的标签中获得required="true"
?
答
试试这个....
f.select :category, ['Analytics','Commerce',..., 'Web'], { :include_blank => '-- Select One --' }, :required => true
+0
我其实只是想通了!我在':required => true'附近放置了花括号,但我不确定它们是否必要。感谢您的帮助。 – 2013-04-22 05:56:06
答
我不确定它是否能完成你想要的,但我有两种解决方案。你可以使用简单的宝石。
OR
的风采吧:
<label class="required">Category</label>
的CSS中:
label.required:after{content:"*"}
答
对于导轨4
f.select :category,
['Analytics','Commerce',..., 'Web'],
{
include_blank: '-- Select One --' ,
required: true
}
http://stackoverflow.com/questions/11951873/rails-select-required 我此认为同样的问题 – 2013-04-21 20:58:17
@leef把{:需要=>真}代替:需要=> true给出语法错误,{:prompt =>' - Select One - ',:required => true}会呈现页面,但在我的选择标记中没有必需的=“true”。 – 2013-04-22 05:45:12