【发布时间】:2012-01-13 17:07:35
【问题描述】:
我的用户设置中有一个表单,用于显示或隐藏用户个人资料的一部分。表单是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
我的表格:
<%= 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();
});
});
【问题讨论】: