1

以下代码:

<div id="vote_form">
  <%= form_remote_tag :url => story_votes_path(@story) do %>
    <%= submit_tag 'shove it' %>
  <% end %>
</div>

给出编译错误

而如果第一个 <%=被替换为<%,那么一切正常。我认为它们只是“显示”或“不显示”不同,但为什么它实际上会导致编译错误?

错误是:

> SyntaxError in Stories#show
> 
> Showing
> app/views/stories/show.html.erb where
> line #17 raised:
> 
> compile error C:/Software
> Projects/ror/shov12/app/views/stories/show.html.erb:17:
> syntax error, unexpected ')' ...
> story_votes_path(@story) do ).to_s);
> @output_buffer.concat ...
>                               ^ C:/Software
> Projects/ror/shov12/app/views/stories/show.html.erb:23:
> syntax error, unexpected kENSURE,
> expecting ')' C:/Software
> Projects/ror/shov12/app/views/stories/show.html.erb:25:
> syntax error, unexpected kEND,
> expecting ')'
4

3 回答 3

2

您会在错误消息中看到它。使用<%= ... %>erb 时会将其替换为 (...).to_s。当 a 后面跟着一个右括号时,Ruby 会感到困惑do,它需要某种类型的块。

于 2010-05-24T09:53:24.287 回答
1

看看这个

当您使用 form_tag 时,它会调用 FormHelper 类的 form_tag 方法。此帮助方法返回 html 取决于您的代码。

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 331
331:       def form_remote_tag(options = {}, &block)
332:         options[:form] = true
333: 
334:         options[:html] ||= {}
335:         options[:html][:onsubmit] =
336:           (options[:html][:onsubmit] ? options[:html][:onsubmit] + "; " : "") +
337:           "#{remote_function(options)}; return false;"
338: 
339:         form_tag(options[:html].delete(:action) || url_for(options[:url]), options[:html], &block)
340:       end

从这里你会知道编译器期待块并且当它没有得到它时它会出错。

于 2010-05-24T10:00:32.850 回答
0

这实际上是在 3 之前的 Rails 版本如何处理 ERB 模板中的 Ruby 块的不一致。因为form_for助手将 HTMLform元素插入到内容周围的视图中,所以它应该在其 ERB 标记中使用等号,Rails 3 修复了这个问题。

于 2010-05-24T09:56:43.567 回答