Rails if statement syntax

血红的双手。 提交于 2019-12-21 07:02:50

问题


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

回答1:


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.




回答2:


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



回答3:


try this

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


来源:https://stackoverflow.com/questions/6706035/rails-if-statement-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!