2012-01-14 96 views
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 

回答

0

Try:

@profile.show_hometown == false || @profile.show_hometown == "0" 

该复选框将返回一个“0”值,而不是false,显然ruby并不总是将“0”理解为false(我的开发平台理解为“0”为false,但我的生产平台没有,我不知道原因;它可能是ruby版本)。