4
ユーザーの設定の一部を表示または非表示にするフォームがあります。フォームはで、クリックするとjQueryを使用してフォームを送信します。これはすべてうまくいきます。チェックボックスをクリックすると、ブール値がtrue
からfalse
に、またはその逆に切り替わります。しかし、私のブール値がfalse
の場合、チェックボックスをチェックして表示したいと思います。どのように私は下の私のコードを与えてそれを行うのですか?ブール値がfalseの場合、チェックボックスをチェックしてレンダリングします。
プロフィールのupdate_attribute
への私のコントローラのアクション:
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
マイ形式:
<%= form_tag({:action => "edit_show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do %>
<%= check_box_tag :show_hometown %>
<%= @user.profile.hometown %>
<% end %>
jQueryの私は、フォームを送信するために使用しています:
$(function(){
$(':checkbox').live('click',function() {
$(this).closest('form').submit();
});
});
実際、 '@ user.profile.show_hometown'がfalseの場合、チェックボックスをオンにします。また、初心者としては、私の論理についてどういうことを混乱させているのか、どうすればよりうまくいくのかが不思議です。 – tvalent2
これは実際には機能しました。何らかの理由でそれは最初ではなかったが、それはうまくいった。私はまだ論理について興味がある。ありがとう! – tvalent2
あなたが知っているように、check_box_tagはhtmlチェックボックスを作成します。 –