Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal nesting: nesting within plain text is illegal

I'm new to HAML and I have an error: Illegal nesting: nesting within plain text is illegal.. Currently, I'm trying to change my erb to haml but it is not working. Here is my 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 %>

My HAML is:

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

What is wrong? Why is that error showing?

like image 373
Cherenkov Avatar asked Sep 27 '15 23:09

Cherenkov


1 Answers

The problem is in your script tag:

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

The line container: "payment-form", is indented more than the previous line, and Haml is trying to parse it as a block or as the contents of a tag, but the previous line is plain text and doesn’t have either of those.

To fix it you can use the :javascript filter which allows mixed indentation below it:

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

The :javascript filter also adds the <script> tag. If you have some reason to control the attributes of this tag you could use the :plain filter with your own tag line:

%script{:data => {:example => "Foo"}}
  :plain
    var clientToken = "#{@client_token}";
    braintree.setup(clientToken, "dropin", {
      container: "payment-form",
      coinbase: { container: "coinbase-container-id" }
    });
like image 151
matt Avatar answered Sep 20 '22 22:09

matt