这对我来说是一个很难回答的问题,所以我会先分享代码:
<div class="col-xs-12">
We operate on a pay what you want model. Keep in mind that
<% if action_name == "outdoors" %>
<%= outdoors_donation.html_safe %>
<% elsif action_name == "snowsports" %>
<%= snowsports_donation.html_safe %>
<% end %>
Below, you'll find information on how we calculated your order's suggested donation of $<span id="modal-sentence"></span>.
</div>
而不是笨拙的if语句,我只是能够将action_name直接注入到被调用的变量中......就像
<%= "#{action_name}_donation".html_safe %>
但不确定这实际上是什么
谢谢!
答案 0 :(得分:2)
您可以在ruby中使用send
来发送您为其提供名称的方法。这允许你进行插值:
<%= self.send("#{action_name}_donation").html_safe %>
在此上下文中,self
将是视图实例,与调用outdoors_donation.html_safe
或snowsports_donation.html_safe
或其他内容相同。