尝试在部分使用条带时的NoMethodError / Undefined方法

时间:2016-04-07 17:37:53

标签: ruby-on-rails stripe-payments

我已将Stripe实施到我的应用程序中,但我试图将“升级成员资格”选项转移到部分中。我把代码放在一个部分,并试图在我的用户#show视图中使用partial,页面将无法正确加载,它给我下面列出的错误。当我访问我的费用#new view(这是我在尝试将所有内容移到我的用户#show view之前设置的默认视图)时,视图加载并没有问题。但是当我把它扔到局部并尝试在我的用户#show中使用partial时,我得到一个NoMethodError/Undefined Method而我似乎无法找出原因。

下面是错误和我的代码(我在错误消息中突出显示的行中添加了注释)。如果需要添加任何其他代码/信息,请告诉我。提前感谢您的帮助!

错误

NoMethodError in Users#show
undefined method `[]' for nil:NilClass

<h4>Upgrade To Premium</h4>
    <script class='stripe-button' src="https://checkout.stripe.com/checkout.js"
                                  data-key="<%= @stripe_btn_data[:key] %>" #highlighted line in the error
                                  data-amount=<%= @stripe_btn_data[:amount] %>
                                  data-description="<%= @stripe_btn_data[:description] %>"
                                  data-email="<%= current_user.email %>" >

_form Partial

<% if current_user.premium? %>
  <%= render partial: "charges/downgrade" %>
<% else %>
  <%= form_tag charges_path do %>
    <h4>Upgrade To Premium</h4>
    <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] %>"
                                  data-email="<%= current_user.email %>" >
    </script>
  <% end %>
<% end %>

用户#表明

<div class="container">
  <h2><%= @user.email %></h2>
    <% @wikis.each do |wiki| %>
    <ul>
      <li><%= link_to wiki.title, @wiki %></li>
    </ul>
    <% end %>
</div>
<br />
<div class="container">
  <%= render partial: "charges/form" %>
</div>

我还有另外一部分我正在使用的部分内容,我发现它有错误,不确定它是否相关,但这只是以防万一。

降级部分

<%= button_to 'Downgrade Membership', user_downgrade_path(current_user), class: 'btn' %>

为控制器充电

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]
    )

    charge = Stripe::Charge.create(
      customer: customer.id, # Note -- this is NOT the user_id in your app
      amount: 15_00,
      description: "Premium Membership - #{current_user.email}",
      currency: 'usd'
    )

    current_user.update_attributes!(role: 'premium')

    flash[:notice] = "Thank you for upgrading to a Premium Membership, #{current_user.email}!"
    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[:alert] = e.message
    redirect_to new_charge_path
  end

  def new
    if user_signed_in?
      @amount = 15_00
      @stripe_btn_data = {
        key: "#{ Rails.configuration.stripe[:publishable_key] }",
        description: "BigMoney Membership - #{current_user.email}",
        amount: @amount
      }
    else
      redirect_to root_path
      flash[:notice] = "You must be signed in to do that."
    end
  end
end

1 个答案:

答案 0 :(得分:0)

结束了愚蠢和容易的事情。我需要在@stripe_btn_data中定义user_controller,因为我在用户视图中使用了部分。