1

I was curious: in an ERB file, when passing a block to a view helper, why does this work:

<%= div_for @thing do |x| %>
  <%= x %>
<% end %>

while this doesn't?

<%= div_for @thing {|x| x.to_s} %>

In Ruby, do...end is exactly the same as {...}, so why not in ERB? Note aside: I can use x on its own on the second line above because its .to_s method returns the field I want to render. Sorry if this has been asked before, I wasn't able to find a similar question (found a similar answer though).

4

1 回答 1

0

我怀疑第二个代码块与以下代码完全相同:

<%= div_for @thing do |x| %>
  <% x %>
<% end %>

因为 x 上没有“=”,所以它不会在您的视图中输出。尝试 :

<%= div_for @thing {|x| concat x.to_s} %>
于 2012-08-22T14:41:53.097 回答