在没有<%=的ERB中打印?

Che*_*eng 27 ruby-on-rails erb

有时在<%%>中打印更方便.如何在Rails中做到这一点?

Oma*_*shi 46

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-concat

应该是你在寻找什么.

例如以下声明使用concat:

<% concat "Output" %>
Run Code Online (Sandbox Code Playgroud)

相当于:

<%= "Output" %>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,`concat`帮手 (2认同)
  • 为什么不能像`puts`那样`echo`在PHP中工作? (2认同)
  • STDOUT用Rails转到其他地方,你可能会*做一些令人讨厌的事情,以便所有STDOUT都转到你的erb模板,但是,不要. (2认同)

Evo*_*lve 7

在ERB中:<%%>表示此处有Ruby代码需要解释.<%=%>表示输出ruby代码,即显示/打印结果.

因此,如果要在标准ERB文件中输出,似乎需要使用extra =符号.

否则,您可以查看ERB的替代方法,这需要更少的语法,...也许尝试类似HAML的东西.http://haml-lang.com/tutorial.html

Example:

# ERB
<strong><%= item.title %></strong>

# HAML
%strong= item.title
Run Code Online (Sandbox Code Playgroud)

那更方便吗?