2016-04-04 47 views
1

如何將布爾值賦予會話屬性並從另一個地方讀取/檢查它?在jRuby/ruby​​ on rails中爲會話屬性指定布爾值

這是正確的方法嗎?

勘定:

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

回答

2

如果你想明確地檢查它是true,不truthy

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

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

如果truthy(也不false既不nil)就足夠了:

<% if session[:contacts_available] %> 

問號意在方法名稱中使用結局按照慣例,人們不應該把它放在「以防萬一。 「

1

是的,你可以指定boolean爲會話,在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 
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 %>