1
对于更新用户的配置文件属性:show_hometown
的表单,我有一个check_box_tag。当我提交表单以切换:show_hometown
的值时,无论属性如何变化,复选框输入的值都为“1”。有人能帮我弄清楚我做错了什么吗?check_box_tag在属性值为true或false时不更改值
这里是我的形式:
<%= form_tag({:action => "edit_show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do %>
<%= check_box_tag :show_hometown, 0, 1 %>
<%= @user.profile.hometown %>
<% end %>
下面是从那里我更新的属性在控制器中的作用:
def edit_show_hometown_settings
@profile = current_user.profile
if @profile.show_hometown == true
if @profile.update_attributes(:show_hometown => false)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
elsif @profile.show_hometown == false
if @profile.update_attributes(:show_hometown => true)
redirect_to settings_path
else
redirect_to settings_path, :notice => 'Oops, something went wrong. Please try again.'
end
end
end
最后,我用来创建:show_hometown
属性迁移:
t.boolean :show_hometown, :default => true