Reputation: 1829
I have it setup in the view for users to unsubscribe from notifications. They can successfully unsubscribe with the current code, but they cannot resubscribe however. The boolean value once changed to 1
will not revet back to NULL
<% if @user.no_email == true %>
<%= form_for(@user) do |f| %>
<div class="forms">
<%= f.check_box :no_email, :value => nil %>
<%= f.submit 'Unsubscribe from all email notifications' %>
</div>
<% end %>
<% end %>
<% if @user.no_email.nil? %>
<%= form_for(@user) do |f| %>
<div class="forms">
<%= f.check_box :no_email, :value => 1 %>
<%= f.submit 'Subscribe to email notifications' %>
</div>
<% end %>
<% end %>
Upvotes: 0
Views: 98
Reputation: 1395
To simplify matters:
<%= form_for(@user) do |f| %>
<div class="forms">
<%= f.check_box :no_email, :value => @user.no_email %>
<%= f.submit @user.no_email ? 'Unsubscribe' : 'Subscribe' %>
</div>
<% end %>
This will change values from false to true.
Upvotes: 1
Reputation: 30445
Don't use == true
and .nil?
to test boolean values. If the values is false
, that .nil?
check will fail. Just do something like:
if @user.no_email
and
if [email protected]_email
Upvotes: 0