Newbie: Triple if-condition syntax on VIEW

I would like to have if if logic:

var == 10 ? "10″ : "Not 10 

in Rails VIEW. I tried the following:

 <%= session[:id]=="out"? link_to "Sign in", login_path : link_to "Sign out", logout_path%> 

I know this looks weird, and no wonder it doesn't work. So, if I would like to use trernary if condition on VIEW, then what is the correct way to do in my case?

--------- Another condition ---------

I would like to have two "link_to" in else state

----- The error message I received is --------

 compile error syntax error, unexpected tSTRING_BEG, expecting kDO or '{' or '(' ...ession[:id]=="out" ? link_to "Sign in", 
+7
source share
2 answers

Try this (the only difference is the space between " and ? And using parentheses)

 <%= session[:id]=="out" ? link_to("Sign in", login_path) : link_to("Sign out", logout_path) %> 

In brackets in brackets are optional, they are necessary to maintain the priority of the operator in some cases.

Triple IMHO statements are hard to read. You can also do something more verbose:

 <%= link_to("Sign in", login_path) if session[:id] == "out" %> <%= link_to("Sign out", logout_path) if session[:id] != "out" %> 
+12
source
 session[:id]=="out"? 

It looks wrong. It should be

 session[:id]=="out" ? 

By the way, if you need more links in another section, switch to if if else. It could be cleaner:

 <% if condition %> link <% else %> link link <% end %> 
+3
source

All Articles