无法在Haml,Rails中渲染数据属性

时间:2015-09-21 03:28:49

标签: ruby-on-rails haml erb

我一直在尝试将以下内容从html.erb转换为html.haml,但事情并没有正确呈现100%。具体来说,我想知道如何在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]}

在我尝试在Haml中重写代码之前,我原来的html.erb文件如下所示。

在Haml之前(我的页面应该呈现的方式):

Correct

然而,在尝试改为Haml之后,我并没有100%成功地以完全相同的方式呈现页面

在Haml之后(注意数据量($ 15.00)和数据描述(BigMoney Membership)没有显示)

Incorrect

正确呈现此页面的正确Haml语法是什么?

更新

我试图从charge_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

1 个答案:

答案 0 :(得分:2)

使用HAML

实现ERB的相同HTML输出
= 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"}