check_box_tag在属性值为true或false时不更改值

时间:2012-01-14 01:53:24

标签: ruby-on-rails-3 checkbox

我有一个表单的check_box_tag,用于更新:show_hometown的用户个人资料属性。当我提交表格以切换:show_hometown的值时,无论属性如何变化,复选框输入的值都是“1”。有人可以帮我弄清楚我做错了吗?

这是我的表格:

<%= 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

1 个答案:

答案 0 :(得分:0)

尝试:

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

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

相关问题