在 rails 上为 jRuby/ruby 中的会话属性分配布尔值

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

如何将布尔值分配给会话属性并read/check它来自另一个地方?

这是正确的方法吗?

正在分配:

   <% session[:contacts_available]=true %>

检查值:

   <% 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 %>    

如果您想明确检查它是 true,而不是 truthy

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

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

如果 truthyfalsenil)都不够:

 <% if session[:contacts_available] %> 

按照约定,问号用于方法名结尾,不应该“以防万一”。

是的,您可以将布尔值分配给会话,以在 if 语句中检查它,删除 ?

  session[:contacts_available] ? "Found" : "Not Found"

 OR

   <% if session[:contacts_available] %> 
       <p> Yeah Contact Found </p>    
   <% else %>  
       <p>Contacts not found </p>
   <% end %>    

布尔值:

true == true # returns true

false == true # returns false

如果语句:

#session[:contacts_available] = true

if true 
   puts "True"
else 
   puts "false"
end

你可以试试。你不应该在 session[:contacts_available]?

中要求 ?

我想你想检查它是否存在,所以 if true 是执行块,否则执行另一个

 <% 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 %>