2012-01-14 6 views
1

私は、ユーザプロファイル属性:show_hometownを更新するフォームのcheck_box_tagを持っています。フォームを送信して:show_hometownの値を切り替えると、属性の変更に関係なく、チェックボックス入力の値は「1」になります。誰かが私が間違っていることを理解するのを助けることができますか?ここでcheck_box_tagは、属性値がtrueまたはfalseのときに値を変更しません。

は私のフォームです:

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属性を作成するために使用される移行:

<%= form_tag({:action => "edit_show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do %> 
<%= check_box_tag :show_hometown, 0, 1 %> 
<%= @user.profile.hometown %> 
<% end %> 

ここで私は属性を更新するよどこからコントローラ内のアクションがあります:

t.boolean :show_hometown, :default => true 

答えて

0

してみてください。

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

チェックボックスはfalseではなく "0"の値を返し、明らかにルビは "0"を常に偽と解釈しません(私の開発プラットフォームは "0"を偽と認識しますが、私はその理由を知らない。それはおそらくルビーバージョンです)。

関連する問題