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

时间:2016-04-04 07:09:13

标签: ruby-on-rails ruby jruby jrubyonrails

如何为会话属性分配布尔值并从其他位置读取/检查?

这是正确的方法??

分配:

Allow-Origin' header is present on the requested resource. Origin   'https://chat.mydomain.nl' is therefore not allowed access. The response had HTTP status code 503.

检查值:

   <% session[:contacts_available]=true %>

3 个答案:

答案 0 :(得分:2)

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

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

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

如果truthyfalse也不nil}就足够了:

 <% if session[:contacts_available] %> 

问题标记按照惯例用于方法名称结尾,不应该将其“以防万一”。

答案 1 :(得分:1)

是的,你可以为会话分配布尔值,在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

答案 2 :(得分:1)

你可以试试。您不应要求?

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