新手问题 表单中 select 的 class 丢失了么

yuanrenkai · 2015年12月22日 · 最后由 lehug 回复于 2016年03月12日 · 1670 次阅读

情况说明:表单中包含 select 输入框,为该输入框设置了 class,但是解析出来的表单中 select 元素 不存在 class 属性 求大能指导,指导

表单

<%= form_for @user do |f| %>
 .
 .
<%=  f.label :status %>
<%= f.select :status , user_status_options(user) , :include_blank => true , :class => 'form-select'
 .
 .
<% end  %>

生成 html 源码

<form>
 .
 .
  <select name="user[status]" id="user_status" ><option value=""></option>
  <option value="1">living</option>
  <option value="0">dead</option>
  <option value="3">unkown</option></select>
 .
 .
</form>

这个:include_blank => true 用{}wrap 一下

http://edgeapi.rubyonrails.org/#method-i-select 你没有写清楚 options 和 html_options 把:class => 'form-select'加个{}试试

<%= form_for :post do |f| %>
    <%= f.select :status, ['Item1'], {}, {:class => 'my-select'} %>
<% end %>
<select class="my-select" name="post[status]" id="post_status"><option value="Item1">Item1</option></select>

@stone @zzxworld 正解,谢谢 @adamshen 同样,谢谢

感谢,终于找到答案了,这个加 class 属性郁闷了好久

需要 登录 后方可回复, 如果你还没有账号请 注册新账号