代码: <%= f.input :name,:label => "姓名",:label_html => { :class =>'label' },:input_html =>{:class =>"text_field"},:wrapper_html =>{:class =>'group'}%>
执行后 label、input、wrapper 的 class 属性仍然是默认值。求解答
代码:
<%= 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="✓" /><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>
????是我没说清楚???
<%= 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="✓" /><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
中),这个文件已经有很多文档了