ruby-on-rails - 为什么 ERB 会出现此错误?

标签 ruby-on-rails ruby

我想从数据库输出一些代码。

我创建了一个表Page,其中包含列titlecontent。在content中,我放置了表单代码。我正在尝试将其输出到 View 中。

<%= form_for :article do |f| %>
<div class="form-group">
    <%= f.label :title, "Title" %>
    <%= f.text_field :title, class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= f.label :price, "Price" %>
    <%= f.number_field :price, value: 1, class: 'form-control', min: 1 %>
  </div>

  <div class="form-group">
    <%= f.label :stock, "Stock" %>
    <%= f.number_field :stock, value: 1, class: 'form-control', min: 1 %>
  </div>

  <div class="form-group">
    <%= f.file_field :image %>
  </div>

  <%= f.submit "Save", class: 'btn btn-success' %>
<% end %>

在我看来,我有:

<% @pages.each do |page| %>
  <%= raw ERB.new(page.content).result(binding) %>
<% end %>

我得到了一个错误:

(erb):1: syntax error, unexpected ')' t(( form_for :article do |f| ).to_s); _erbout.concat "\r\n<d ^ (erb):22: syntax error, unexpected keyword_end, expecting ')' ; end ; _erbout.force_encoding(__EN ^ (erb):22: syntax error, unexpected end-of-input, expecting ')' t.force_encoding(__ENCODING__) ^

我陷入了这个错误。我认为错误是在 ERB 中,但我不知道如何修复它。如果你能帮助我,那就太好了。

最佳答案

http://timelessrepo.com/block-helpers-in-rails3谈论这个:

<%= box do %> is simply not valid ERB. While there’s no written spec for ERB, there are some basic rules which every implementation of ERB can follow.

The first rule is that the expression in <%= =%> must be a complete expression. A complete expression is an expression which you can pass directly into eval without getting a syntax error. Or you could say that it’s a piece of Ruby code which you can place parenthesis around and it still ends up as valid Ruby:

eval("f.text_field")  # => Works fine 
( f.text_field )      # => Valid

eval("box do")        # => SyntaxError
( box do )            # => Invalid 

The expression in <% %> on the other hand only needs to be a subexpression. A subexpression is something which by itself is an invalid expression, but becomes valid if there’s another subexpression below or above it which completes it. You could also say that it’s a piece of Ruby code which you can place semicolons around:

eval("box do")    # => SyntaxError
; box do ;        # => Valid (as long as there is an `end` later) 

As you can see, box do is a subexpression, but not a complete expression. Therefore, in normal ERB, you can put it inside <% %>, but not <%= %>.

事实上,如果您进入 ERB.new(page.content) 对象,您会看到它首先尝试执行以下操作:

#coding:UTF-8
_erbout = ''; 
_erbout.concat(( form_for :article do |f| ).to_s); 

所以它会提示,因为它不知道如何计算表达式 form_for :article do |f|就其本身而言。

Rails 在进行自己的渲染时会在幕后进行特殊处理以使这些工作正常进行,但这不是来自 ERB 的东西。

(类似问题:Why is this an error with ERB?)

关于ruby-on-rails - 为什么 ERB 会出现此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49608111/

相关文章:

sql - 如何在 Rails 4 中构建跨多个表的高效选择命令?

ruby-on-rails - ruby rails : Custom routes for search results

ruby-on-rails - rails 3 : How do I delay content submission so users can't spam rapidly?

ruby-on-rails - Rails + Passenger : Invalid Route name, 已在使用中

ruby-on-rails - "rake assets:precompile"给出 punc 错误

Mysql2::错误:字段列表中的列不明确

ruby-on-rails - 如何将数据从 Matlab 发送到 Rails

ruby-on-rails - 更改参数[ :id] in Ruby on Rails

ruby-on-rails - has_many :through broke some code

Ruby 1.9 const_defined? ("Timeout") 当 Timeout 不在常量列表中时返回 true