【发布时间】:2013-10-02 08:54:09
【问题描述】:
我正在创建一些用户可以单击以填充文本区域的消息建议。显示时,通用字符串必须与用户的详细信息一起呈现。
这是我想要做的,但是这个例子使用了 html 编码的消息,这些消息不是从数据库中获取的:
<ul>
<li><a><%= "#{@user.name} is the best" %></a></li>
<li><a><%= "#{@user.name} is the worst" %></a></li>
<li><a><%= "I think #{@user.name} is the best" %></a></li>
<li><a><%= "I think #{@user.name} is the worst" %></a></li>
</ul>
我希望能够将带有“占位符”的通用字符串存储在数据库中,并且只计算视图中的值。
这就是我尝试在数据库中(在种子文件中)创建字符串的方式
Suggestion.create(message: '#{@user.name} is the best')
Suggestion.create(message: '<%= #{@user.name} %> is the best')
Suggestion.create(message: '<%= @user.name %> is the best')
在视图中我有一个迭代
<%= suggestion.message %>
我正在尝试在渲染之前将 ruby 代码添加到视图中。可能是个愚蠢的想法。
这是在 html 源代码中显示的内容
<%= @user.name %> is the best
<%= #{@user.name} %> is the best
#{@user.name} is the best
这里有一些类似的东西,但它附加了无法使用的消息,因为变量在每条消息中的不同位置:
<ul>
<% @suggestions.each do |message| %>
<li><a><%= "#{@user.name} message" %></a></li>
<% end %>
</ul>
【问题讨论】:
-
您的视图可能使用了
.html扩展而不是.html.erb -
它使用的是 .erb 扩展名
-
<a>标签在哪里?在代码中的字符串中,还是在视图中? -
当您使用浏览器点击该页面时,您的日志文件会显示什么?
-
您需要向我们展示更多代码。你可以将你的项目提交到 github 以便我们审核吗?
标签: ruby-on-rails ruby liquid