What is the difference between <%= … %> and <% … %> in Ruby on Rails

前端 未结 3 1824
难免孤独
难免孤独 2020-12-06 21:18

Does it have something to do with output?

So, <%= ...code... %> is used for outputting after code is executed, and <% ...code... %>

3条回答
  •  死守一世寂寞
    2020-12-06 21:44

    Both execute the Ruby code contained within them. However, the different is in what they do with the returned value of the expression. <% ... %> will do nothing with the value. <%= ... %> will output the return value to whatever document it is executed in (typically a .erb or .rhtml document).

    Something to note, <%= ... %> will automatically escape any HTML contained in text. If you want to include any conditional HTML statements, do it outside the <% ... %>.

    <%= "
    " if need_line_break %> <% if need_line_break %>
    <% end %>

提交回复
热议问题