gpt4 book ai didi

ruby-on-rails-3 - 如果 bool 值为 false,则获取复选框以呈现为选中状态

转载 作者:行者123 更新时间:2023-12-04 17:26:15 25 4
gpt4 key购买 nike

我有一个在我的用户设置中工作的表单,用于显示或隐藏用户个人资料的一部分。表格是 check_box_tag当它被点击时,我使用 jQuery 提交表单。这一切都运行良好。单击复选框可切换 bool 值 truefalse或相反亦然。但是,如果我的 bool 值是 false,我希望复选框显示为已选中。 .鉴于我的代码,我该怎么做?

我的 Controller 对 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();
});
});

最佳答案

我对你的逻辑有点困惑,但我想你明白了。只需根据需要更改 true 和 false。如果@user.profile.hometown 设置为false,这会将复选框设置为选中状态。

<%= check_box_tag :show_hometown , 1 , @user.profile.hometown ? false : true %>

关于ruby-on-rails-3 - 如果 bool 值为 false,则获取复选框以呈现为选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8846180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com