ruby-on-rails – 非法嵌套:在纯文本中嵌套是非法的

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 非法嵌套:在纯文本中嵌套是非法的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是HAML的新手,我有一个错误:非法嵌套:在纯文本中嵌套是非法的..目前,我正在尝试将我的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,: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,coinbase: { container: "coinbase-container-id" }
    });

怎么了?为什么会出现错误

解决方法

问题出在您的脚本标记中:
%script{:charset => "utf-8",:type => "text/javascript"}
  var clientToken = "#{@client_token}";
  braintree.setup(clientToken,{
    container: "payment-form",coinbase: { container: "coinbase-container-id" }
  });

行容器:“payment-form”比前一行缩进,并且Haml试图将其解析为块或标记内容,但前一行是纯文本,并且没有任何一个那些.

要修复它,你可以使用:javascript filter,它允许在它下面混合缩进:

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

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

%script{:data => {:example => "Foo"}}
  :plain
    var clientToken = "#{@client_token}";
    braintree.setup(clientToken,coinbase: { container: "coinbase-container-id" }
    });
原文链接:https://www.f2er.com/ruby/265367.html

猜你在找的Ruby相关文章