开发者

If else not working because it contains end within

I have a a helper that contains a simple on and off switch. I know I have it working because it's working on other pages. However, on this particular page it won't work.. I think its because theres an end within the if else, so it ends the if else early. Here's the code:

I believe this part is working:

<% if popup == "off" %>
       <% content_for :main do %>开发者_开发问答
<% end %>

This part not so much:

<% if popup == "off" %>
       <% end %>  << this end should be displayed if popup = off 
<% end %>


You could do this:

<% if popup == "off" %>
       <%= "<% end %>" %>  << this end should be displayed if popup = off 
<% end %>

or try this:

<% if popup == "off" %>
       &lt;% end %&gt;  << this end should be displayed if popup = off 
<% end %>


If you just want the word end to be displayed, don't enclose it in tags. Anything enclosed in tags is interpreted as Ruby code, anything not is printed exactly as it is.

<% if popup == "off" %>
   end  << this will now be interpreted as text, not ruby code
<% end %>


ERB (and Ruby) doesn't work like that.

I think you're treating it like you are trying to end an HTML tag instead of an end to a Ruby block, and that you want everything in between those two code segments to run in the content_for block.

Here's what you need. Everything in between will be included in the content_for block:

<% if popup == "off" %>
  <% content_for :main do %>
    your block code will be evaluated here.
  <% end %>  
<% end %>


Seems all the suggestions of doing <%= "<% end %>" %> results in a syntax error.. May seem like easy way out by ended up just restructuring my app and got rid of the requirement of <% content_for :main do %>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜