tva*_*nt2 5 checkbox ruby-on-rails-3
我有一个表单在我的用户设置中工作,用于显示或隐藏用户个人资料的一部分.表单是a check_box_tag,当单击它时,我使用jQuery提交表单.这一切都运作良好.单击该复选框可将布尔值从中切换为true,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
Run Code Online (Sandbox Code Playgroud)
我的表格:
<%= form_tag({:action => "edit_show_hometown_settings", :controller => "profiles"}, :html => {:multipart => true }) do %>
<%= check_box_tag :show_hometown %>
<%= @user.profile.hometown %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
我用来提交表单的jQuery:
$(function(){
$(':checkbox').live('click',function() {
$(this).closest('form').submit();
});
});
Run Code Online (Sandbox Code Playgroud)
Kri*_*ndt 10
我对你的逻辑感到困惑,但我想你明白了.只需根据需要改变真假.如果@ user.profile.hometown设置为false,则会将复选框设置为选中状态.
<%= check_box_tag :show_hometown , 1 , @user.profile.hometown ? false : true %>
Run Code Online (Sandbox Code Playgroud)