非法嵌套:在纯文本中嵌套是非法的

时间:2015-09-27 23:40:37

标签: ruby-on-rails haml

我是HAML的新手,我有一个错误:Illegal nesting: nesting within plain text is illegal.。目前,我试图将我的erb改为haml,但它无法正常工作。这是我的问题。

<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<h2 class="mbs">New Subscription
</h2>
<%= simple_form_for :subscription, :url => subscribe_subscription_path(@plan.id), :id => "checkout-form" do |f| %>
  <% if current_user and !current_user.has_payment_info? %>
    <%= render 'customer_form'%>
  <% end %>
  <br/>
  <p> Please enter payment details: </p>
  <div id="payment-form"></div>
  <div id="coinbase-container-id"></div>
  <div class="form-actions">
    <%= f.submit t(:subscribe), :class => 'btn btn-primary' %>
  </div>
  <script charset="utf-8" type="text/javascript">
    var clientToken = "<%= @client_token %>";
    braintree.setup(clientToken, "dropin", {
      container: "payment-form",
      coinbase: { container: "coinbase-container-id" }
      });
  </script>
<% end %>

我的HAML是:

%script{:src => "https://js.braintreegateway.com/v2/braintree.js"}
%h2.mbs
New Subscription
= simple_form_for :subscription, :url => subscribe_subscription_path(@plan.id), :id => "checkout-form" do |f|
  - if current_user and !current_user.has_payment_info?
    = render 'customer_form'
  %br/
  %p Please enter payment details:
  #payment-form
  #coinbase-container-id
  .form-actions
    = f.submit t(:subscribe), :class => 'btn btn-primary'
  %script{:charset => "utf-8", :type => "text/javascript"}
    var clientToken = "#{@client_token}";
    braintree.setup(clientToken, "dropin", {
      container: "payment-form",
      coinbase: { container: "coinbase-container-id" }
    });

有什么问题?为什么会出现错误?

1 个答案:

答案 0 :(得分:8)

问题出在您的script标记中:

%script{:charset => "utf-8", :type => "text/javascript"}
  var clientToken = "#{@client_token}";
  braintree.setup(clientToken, "dropin", {
    container: "payment-form",
    coinbase: { container: "coinbase-container-id" }
  });

container: "payment-form",缩进比前一行缩进,并且Haml尝试将其解析为块或标记的内容,但前一行是纯文本,并且没有那些。

要解决此问题,您可以使用允许在其下方使用混合缩进的:javascript filter

:javascript
  var clientToken = "#{@client_token}";
  braintree.setup(clientToken, "dropin", {
    container: "payment-form",
    coinbase: { container: "coinbase-container-id" }
  });

:javascript过滤器还会添加<script>标记。如果您有理由控制此标记的属性,可以将:plain filter与您自己的标记行一起使用:

%script{:data => {:example => "Foo"}}
  :plain
    var clientToken = "#{@client_token}";
    braintree.setup(clientToken, "dropin", {
      container: "payment-form",
      coinbase: { container: "coinbase-container-id" }
    });