为什么ERB出错? [英] Why is this an error with ERB?

查看:93
本文介绍了为什么ERB出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<div class='row'>
  <%= form.field_container :name do %>
    <%= form.label :name, raw('Name' + content_tag(:span, ' *', :class => 'required')) %>
    <%= form.text_field :name, :class => 'fullwidth' %>
    <%= form.error_message_on :name %>
  <% end %>
</div>

为什么会产生以下错误?

Why is this producing the following error?

$ erb -x -T - test.erb | ruby -c
-:3: syntax error, unexpected ')'
...form.field_container :name do ).to_s); _erbout.concat "\n"
...                               ^
-:9: syntax error, unexpected $end, expecting ')'


推荐答案

如果您查看 erb -x -T-t​​est.erb

#coding:ASCII-8BIT
_erbout = ''; _erbout.concat "<div class='row'>\n  "
; _erbout.concat(( form.field_container :name do ).to_s); _erbout.concat "\n"
; _erbout.concat "    "; _erbout.concat(( form.label :name, raw('Name' + content_tag(:span, ' *', :class => 'required')) ).to_s); _erbout.concat "\n"
; _erbout.concat "    "; _erbout.concat(( form.text_field :name, :class => 'fullwidth' ).to_s); _erbout.concat "\n"
; _erbout.concat "    "; _erbout.concat(( form.error_message_on :name ).to_s); _erbout.concat "\n"
; _erbout.concat "  ";  end ; _erbout.concat "\n"
; _erbout.concat "</div>\n"
; _erbout.force_encoding(__ENCODING__)

您可以在第三行看到 do 后跟。 Ruby期望 do end 块,但是得到一个右括号。这就是语法错误的直接原因。

You can see that on the third line, a do is followed by a ). Ruby is expecting a doend block, but gets a closing parenthesis. That’s the immediate cause of the syntax error.

erb 认为错误代码的原因是您使用的是<%= 时,应使用<%。将代码更改为此可修复语法错误:

The reason for erb outtputting bad code is that you are using <%= when you should be using <%. Changing your code to this fixes the syntax error:

<div class='row'>
  <% form.field_container :name do %>
    <%= form.label :name, raw('Name' + content_tag(:span, ' *', :class => 'required')) %>
    <%= form.text_field :name, :class => 'fullwidth' %>
    <%= form.error_message_on :name %>
  <% end %>
</div>

我无法运行此代码来测试更改后输出的内容,但是由 erb 生成的代码似乎可以正常工作:

I can’t run this code to test if it outputs what it should after my change, but the code generated by erb looks like it will work:

#coding:ASCII-8BIT
_erbout = ''; _erbout.concat "<div class='row'>\n  "
;  form.field_container :name do ; _erbout.concat "\n"
; _erbout.concat "    "; _erbout.concat(( form.label :name, raw('Name' + content_tag(:span, ' *', :class => 'required')) ).to_s); _erbout.concat "\n"
# more...



编辑



由于此解决方案显然确实破坏了输出,因此我调查了 mu太短。我检查了 Erubis ,该 Rails 3默认使用,其行为与ERB不同。 erubis -x -T-t​​est.erb 输出的代码(带有未经编辑的原始 test.erb ):

Edit

Since this solution apparently does break the output, I looked into what mu is too short suggested. I checked if Erubis, which Rails 3 uses by default, behaves differently from ERB. The code outputted by erubis -x -T - test.erb (with the original, unedited test.erb):

_buf = ''; _buf << '<div class=\'row\'>
  '; _buf << ( form.field_container :name do ).to_s; _buf << '
'; _buf << '    '; _buf << ( form.label :name, raw('Name' + content_tag(:span, ' *', :class => 'required')) ).to_s; _buf << '
'; _buf << '    '; _buf << ( form.text_field :name, :class => 'fullwidth' ).to_s; _buf << '
'; _buf << '    '; _buf << ( form.error_message_on :name ).to_s; _buf << '
';   end 
 _buf << '</div>
';
_buf.to_s

第三行的问题完全相同,而 erubis -x -T-t​​est.erb | ruby -c 输出相同的语法错误。因此,ERB和Erubis之间的差异可能不是问题。

Line three has the exact same problem, and erubis -x -T - test.erb | ruby -c outputs the same syntax error. So the differences between ERB and Erubis are probably not the problem.

我还尝试了语法检查这段代码从Rails官方文档中

I also tried syntax-checking this piece of code from the official Rails documentation:

<%= form_for(zone) do |f| %>
  <p>
    <b>Zone name</b><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

它得到相同的语法错误。因此,这并不是说您的ERB代码写得不好。您的代码与该示例非常相似。

It gets the same syntax error. So it’s not that your ERB code is badly written; your code is very similar to that example.

目前,我最好的猜测是 erb -x 标志存在缺陷,并且不支持它应该支持的某些功能,该标志将ERB模板转换为Ruby代码而不是直接对其进行评估。尽管现在我已经考虑过了,但是当您输出本身输出文本的块的结果应该正常工作时,我仍然很难想象应该输出什么Ruby代码 。应该在什么时间写入每个输出-首先输出结果,还是首先写入块内容?

At this point my best guess is that erb’s -x flag, which translates an ERB template into Ruby code instead of evaluating it directly, is flawed, and does not support some features it should. Though now that I think about it, I am having trouble imagining exactly what Ruby code should be outputted when you output the result of a block that itself outputs text should work. At what times should each of the outputs be written – the result first, or the block contents first?

这篇关于为什么ERB出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆