新手问题 simple_form html 属性设置还是无效求解答

supermars · 2012年05月15日 · 最后由 xranthoar 回复于 2012年05月31日 · 5452 次阅读

代码: <%= f.input :name,:label => "姓名",:label_html => { :class =>'label' },:input_html =>{:class =>"text_field"},:wrapper_html =>{:class =>'group'}%>
执行后 label、input、wrapper 的 class 属性仍然是默认值。求解答

昨天不是发了一个,由于是 class 单词写错,然后被删除了吗,今天怎么又发上来了?

这个是单词写正确后,还是由问题。html 的属性设置不上。

看似很简单,但就是不知道问题处在那里。顶一下!请熟手指教

#3 楼 @supermars 把你整个表单的代码,生成出来的 HTML 以及 simple_form 的版本贴出来

simple_form 2 吗?

代码:

<%= simple_form_for(@user) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
 <%=f.input :name ,:label_html =>{:class =>"label"},:input_html => { :class=>"text_field"},:wrapper_html => {:class => "group"}%>
 <%=f.input :password %>
 <%=f.input :address %>
 <%=f.input :salary %>
 <%=f.submit  :submit %>
  <% end %>

生成的 html:

 <form accept-charset="UTF-8" action="/users" class="form new_user" id="new_user" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="H5h2qpXsvZtKQuF7qJ6ni3ABJUsj4+lqV7CE1gsYCvo=" /></div>
   <div class="input string optional group">
           <label class="string optional label label" for="user_name">Name</label>
           <input calss="text_field" class="string optional" id="user_name" name="user[name]" size="50" type="text" />
</div>
 <div class="input password optional">
      <label class="password optional label" for="user_password">Password</label>
      <input class="password optional" id="user_password" name="user[password]" size="50" type="password" />
</div>
<div class="input string optional">
      <label class="string optional label" for="user_address">Address</label>
      <input class="string optional" id="user_address" name="user[address]" size="50" type="text" />
</div>
 <div class="input float optional">
     <label class="float optional label" for="user_salary">Salary</label>
     <input class="numeric float optional" id="user_salary" name="user[salary]" step="any" type="number" />
</div>
<input name="commit" type="submit" value="submit" />
</form>

为什么 div、label、input 中的 class 属性没有取代默认的属性?

默认这些默认的 class 属性值,在那里可以修改?

不想再跟你说了,你自己看看我第一条回复说的那句,再仔细检查你的代码

这...略坑略坑

出现一些不会报错但却导致莫名其妙结果的问题,第一反应是不是自己英文拼写出了问题,毕竟英语不是母语

????是我没说清楚???

 <%= simple_form_for @user do |f| %>
  <%= f.input :name, :input_html => { :class => 'special' } %>
  <%= f.input :password, :input_html => { :class => 'special' } %>
  <%= f.input :address, :input_html => { :value => '1' } %>
  <%= f.button :submit %>
<% end %>

生成 html:

<form accept-charset="UTF-8" action="/users" class="form new_user" id="new_user" method="post" novalidate="novalidate"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="H5h2qpXsvZtKQuF7qJ6ni3ABJUsj4+lqV7CE1gsYCvo=" /></div>
<div class="input string optional">
    <label class="string optional label" for="user_name">Name</label>
    <input class="string optional special" id="user_name" name="user[name]" size="50" type="text" />
</div>
  <div class="input password optional">
      <label class="password optional label" for="user_password">Password</label>
      <input class="password optional special" id="user_password" name="user[password]" size="50" type="password" />
</div>
  <div class="input string optional">
      <label class="string optional label" for="user_address">Address</label>
       <input class="string optional" id="user_address" name="user[address]" size="50" type="text" value="1" />
</div>
  <input class="btn" name="commit" type="submit" value="Create User" />
</form>

我是问:为什么 name 这个 input 的 class 属性值是“string optional special”而不是只是”special“ 如果要让他只是‘special’应该如何设置?

@supermars 改默认的 class 到 simple_form 的配置里 (/config/initializers/simple_form.rb中),这个文件已经有很多文档了

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