2015-09-27 122 views
6

我是HAML新手,我有一個錯誤:Illegal nesting: nesting within plain text is illegal.。目前,我試圖將我的erb改爲haml,但它不起作用。這是我的erb。非法嵌套:在純文本中嵌套是非法的

<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" } 
    }); 

有什麼不對?爲什麼會顯示該錯誤?

回答

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" } 
    }); 
+0

非常感謝!這工作! :) – ClauCece