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 체크 박스를 만듭니다. 세 개의 매개 변수가 주어진다 : –