我想从复选框中获取参数并修改值。因此,如果选中该复选框,则该值应在模型 1 中,否则为 0。
每次当我检查模型时,它都会对我说:
#<User id: 1, name: "admin", email: "admin@bl.ch", created_at: "2013-11-04 07:18:34", updated_at: "2013-11-04 07:18:34", password_digest: "$2a$10$Gk7IF2OE1GxxeZdqFXygMezwNrurv.6yemuOFSNjLKFS...", role: nil>
它应该是role: 1
或role: 0
控制器
def create
role = User.new(params[:role]) #get params
# if role.exists?
if role == 0
role = 0
else
role = 1
end
end
html
<% provide(:title, 'Log In') %>
<div class="row">
<div class="span6 offset3">
<%= render 'shared/error_messages' %>
<%= form_for(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email, 'E-Mail' %>
<%= f.text_field :email %>
<%= f.label :password, 'Passwort' %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Passowrt wiederholen" %>
<%= f.password_field :password_confirmation %>
<%= f.label :role, 'Ist Admin?'%>
<%= f.check_box :role %><br><br>
<%= f.submit "Account erstellen", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>