xps15z
xps15z

Reputation: 1829

boolean value not changing back to NULL

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

Answers (2)

pwz2000
pwz2000

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

Alex D
Alex D

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

Related Questions