jlquaccia
jlquaccia

Reputation: 165

Trouble rendering data attributes in Haml, Rails

I've been trying to convert the following from html.erb to html.haml, but things aren't rendering 100% correctly. Specifically I'm wondering how to correctly write the bit of code within the script tags in Haml.

html.erb:

<%= form_tag charges_path do %>
  <h4>So what comes with being a Blocipedia premium member? </h4>
  <p>The ability to create your very OWN private wikis of course!</p>
  <script class='stripe-button' src="https://checkout.stripe.com/checkout.js" data-key="<%= @stripe_btn_data[:key] %>" data-amount=<%= @stripe_btn_data[:amount] %> data-description="<%= @stripe_btn_data[:description] %>" ></script>
<% end %>

html.haml:

= form_tag charges_path do
  %h4 So what comes with being a Blocipedia premium member?
  %p The ability to create your very OWN private wikis of course!
  %script.stripe-button{"data-key" => @stripe_btn_data[:key], :src => "https://checkout.stripe.com/checkout.js", "data_amount" => @stripe_btn_data[:amount], "data_description" => @stripe_btn_data[:description]}

Before I tried to rewrite my code in Haml, my original html.erb file rendered like so below.

Before Haml (The way my page SHOULD render):

Correct

Yet after trying to change to Haml, I was not 100% successful in getting the page to render in exactly the same way

After Haml (notice the data-amount ($15.00) and the data-discription (BigMoney Membership) aren't being displayed)

Incorrect

What is the correct Haml syntax to render this page properly?

Update

I'm trying to grab the data-amount and description from my charges_controller.rb

charges_controller.rb:

class ChargesController < ApplicationController
  def create
    # Creates a Stripe Customer object, for associating with the charge
    customer = Stripe::Customer.create(
      email: current_user.email,
      card: params[:stripeToken]
    )

    # Where the real magic happens
    charge = Stripe::Charge.create(
      customer: customer.id, # Note -- this is NOT the user_id in your app
      amount: Amount.default,
      description: "BigMoney Membership - #{current_user.email}",
      currency: 'usd'
    )

    flash[:notice] = "Thanks for all the money, #{current_user.email}!  You now have a premium Blocipedia account!  Feel free to pay me again."

    current_user.update_attribute(:role, "premium")

    redirect_to root_path # Or wherever

    # Stripe will send back CardErrors, with friendly messages when something goes wrong.
    # This rescue block catches and displays those errors.
  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to new_charge_path
  end

  def new
    @stripe_btn_data = {
      key: "#{ Rails.configuration.stripe[:publishable_key] }",
      description: "BigMoney Membership - #{current_user.name}",
      amount: Amount.default
    }
  end

  def destroy
    if current_user.update_attributes(role: "standard")
      flash[:notice] = "You now have a standard Blocipedia account.  Feel free to upgrade back to premium at anytime!"
      redirect_to root_path
    else
      flash[:error] = "There was an error downgrading your account.  Please contact technical support."
      redirect_to edit_user_registration_path
    end
  end
end

amount.rb:

class Amount
  def self.default
    15_00
  end
end

Upvotes: 0

Views: 635

Answers (1)

bigsolom
bigsolom

Reputation: 2349

To achieve the same HTML output of ERB using HAML

= form_tag charges_path do
  %h4 So what comes with being a Blocipedia premium member?
  %p The ability to create your very OWN private wikis of course!
  %script.stripe-button{data: {key: @stripe_btn_data[:key], amount: @stripe_btn_data[:amount], description: @stripe_btn_data[:description]}, src: "https://checkout.stripe.com/checkout.js"}

Upvotes: 2

Related Questions