Search code examples
ruby-on-railsrubyjrubyjrubyonrails

Assigning boolean value to session attribute in jRuby/ruby on rails


How to assign a boolean value to a session attribute and read/check it from another place?

is this the correct way??

Assigning :

   <% session[:contacts_available]=true %>

Checking the value :

   <% if session[:contacts_available]? %> 
       <p> Donec interdum turpis eget leo lobortis, sit amet lacinia ante vulputate. Maecenas hendrerit 
         euismod nulla in semper. Donec arcu nibh, faucibus at posuere id, dapibus non tellus. </p>

   <% else %>  
       <p> You're logged in as : <%= current_user.email %> <%= link_to "Log Out", logout_path %> </p>
       <p> Welcome to our service. You currently don't have any contact details under your username. 
       Please fill the below form to show the first contact detail of yours.  </p>

   <% end %>    

Solution

  • If you want to explicitly check for it being true, not truthy:

     <% if session[:contacts_available] == true %> 
    

    or

     <% if TrueClass === session[:contacts_available] %> 
    

    If truthy (nor false neither nil) is enough:

     <% if session[:contacts_available] %> 
    

    The question mark is intended to be used in method name endings by convention, one should not put it “just in case.”