2016-04-04 8 views
1

Wie kann man einem Sitzungsattribut einen booleschen Wert zuweisen und es von einem anderen Ort lesen/überprüfen?Zuweisen eines booleschen Werts zum Sitzungsattribut in jRuby/ruby ​​on rails

ist das der richtige weg ??

zuordnen:

<% session[:contacts_available]=true %> 

Überprüfen Sie den Wert:

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

Antwort

2

Wenn Sie explizit überprüfen möchten es true zu sein, nicht truthy:

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

oder

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

Wenn truthy (noch false weder nil) genügt:

<% if session[:contacts_available] %> 

Das Fragezeichen ist beabsichtigt, durch Konvention in Methodennamen Endungen verwendet wird, sollte man setzt es „für den Fall nicht nur. "

1

Ja, Sie können Boolean Sitzung zuweisen, um es in If-Anweisung zu überprüfen, entfernen ?.

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

OR 

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

Boolean:

true == true # returns true 

false == true # returns false 

If-Anweisung:

#session[:contacts_available] = true 

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

können Sie versuchen. Sie sollten nicht ? in session[:contacts_available]?

erfordern Ich glaube, Sie es Anwesenheit wahr so ​​if true überprüfen wollen, ist der Block auch andere

<% 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 %> 
ein ausführen ausführen
Verwandte Themen