English 中文(简体)
检查箱:当归属价值真实或虚假时,标签不会改变价值
原标题:check_box_tag not changing value when attribute value is true or false

我有一份核对表,以更新<代码>用户简介属性: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 %>

这里指控制器从Im更新特性的行动:

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
问题回答

Try:

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

检查箱将退回“0”价值,而不是虚假的,而且表面看来是废墟并不理解“0”为虚假的始终(我的发展平台将“0”视为虚假的,但我的生产平台却不知原因;它很可能是废墟。





相关问题
ASP.NET checkbox changes focus?

I have a checkbox control, and when it s clicked, the focus of the page changes to the top--the window scrolls to the top of the page. Nowhere in the code am I specifying a change in focus, either in ...

ASP.Net MVC - Handle Multiple Checkboxes

Ok, I have a role based permission system in place and would like admin s to be able to edit the permissions for each role. To do this I need to load lots of checkboxes, however I m struggling with ...

checkbox / div toggle

$(".trigger").click(function () { $(this).next(".toggle").slideToggle("fast"); $(this).toggleClass("active"); return false }); HTML: <input type="checkbox" class="trigger"> <div class=...

sticky checkbox

Ii am trying to make a checkbox (to send via email) sticky. To make sure it doesn t make a problem when sent empty I used the following: <input type="checkbox" name="something" value="1"> <...

Tri-state Check box in HTML?

There is no way to have a tri-state check button (yes, no, null) in HTML, right? Are there any simple tricks or work-arounds without having to render the whole thing by oneself?

How to get unselected checkbox?

I have three checkboxes like ch[0], ch[1] and ch[3] (sometimes i have more, or less, it s dinamic) and in PHP i want to get the unselected items also, like this: 0=yes,1=no,3=yes and so on. Can I ...

热门标签