2016-04-04 26 views
1

Bir oturum özniteliğine bir boole değeri nasıl atanır ve başka bir yerden okuma/kontrol etme?Raylarda jRuby/ruby'de boolean değerini oturum özniteliğine atama

Bu doğru yoldur?

atama:

<% session[:contacts_available]=true %> 

Kontrol değeri:

açıkça o true olduğu için kontrol etmek istiyorsanız
<% 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 %>  

cevap

2

değil truthy:

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

veya

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

truthy (ne de false ne nil) yeterliyse:

<% if session[:contacts_available] %> 

soru işareti yöntem adına Kongre tarafından sonlar kullanılmak üzere tasarlanmıştır, kimse “her ihtimale karşı koymak gerekir. ”

1

Evet, oturumunuzu boolean'a atayabilir, if deyiminde kontrol etmek için ?'u kaldırın.

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

OR 

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

Boole:

true == true # returns true 

false == true # returns false 

ifadesi ise:

#session[:contacts_available] = true 

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

Deneyebilirsin. Eğer session[:contacts_available]?

yılında ? gerektirmemelidir ben bunu gerçek varlığını kontrol etmek istiyorum düşünüyorum bu yüzden if true başka blok yürütmek olan diğeri

<% 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 %> 
İlgili konular