Difference between “<%=” and “<%” when mixing ruby with html?

后端 未结 2 459
再見小時候
再見小時候 2021-01-28 05:28

I\'m learning Rails, and i\'m doing a exercise which i have to mix some html with ruby in a view file.

What is the main difference between \" <%= #code %> \" and \" &

相关标签:
2条回答
  • 2021-01-28 05:28
    <%= something which you would like to have displayed in your view %>
    <% something you would like to have hidden, 
    (or something which doesn't display anything in the view) such as a
     conditional statement %>
    
    <% if @post.nil? %>
        <%= render "nilNotify" %>
    <% else %>
        <%= @post.content %>
    <% end %>
    
    0 讨论(0)
  • 2021-01-28 05:53

    <%= 1 + 2 %> will evaluate AND display the result. In this case, you should see 3 in your view.

    <% 1 + 2 %> will evaluate but will not display the result in the view. In this case, you will not see 3 in your view.

    0 讨论(0)
提交回复
热议问题