Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails if statement syntax

I've written the following ERB and am getting a syntax error at the question mark. This helper function from devise currently evaluates as false. What have I missed?

<%= if user_signed_in? %> <%= render 'form' %> <%= end %> 
like image 890
cjm2671 Avatar asked Jul 15 '11 11:07

cjm2671


People also ask

How do you write if condition in Ruby on Rails?

Ruby if...else Statement Notice Ruby uses elsif, not else if nor elif. Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed. An if expression's conditional is separated from code by the reserved word then, a newline, or a semicolon.

What is the difference between if and unless statement in Ruby?

In if statement, the block executes once the given condition is true, however in unless statement, the block of code executes once the given condition is false.

What are conditional statements in Ruby?

Ruby conditional statements. Conditional statements are also known by the name of conditional processing or conditional expressions. They are used to perform a certain set of instructions if a specified condition is met. The conditions are generally of Boolean type and return either true or false as the result.


2 Answers

Try this :

<% if user_signed_in? %>   <%= render 'form' %> <% end %> 

If you do <%= ... %>, it will try to output the thing you put between the tags. But, if you do <% ... %>, then no output is processed, just the code is evaluated. If this is not working, then there is probably something wrong with your user_signed_in? helper method.

like image 174
SteenhouwerD Avatar answered Sep 28 '22 03:09

SteenhouwerD


<%= will try to output your user_signed_in? helper, so try:

<% if user_signed_in? %>   <%= render 'form' %> <% end %> 

or even better (and less confusing):

<%= render 'form' if user_signed_in? %> 
like image 30
Mario Uher Avatar answered Sep 28 '22 03:09

Mario Uher