NoMethodError/Undefined 尝试部分使用 Stripe 时的方法
NoMethodError/Undefined Method While Trying To Use Stripe In Partial
我在我的应用程序中实现了 Stripe,但我正在尝试将我的 "Upgrade Membership" 选项移动到部分。我将代码放在一个部分中,并试图在我的 users#show 视图中使用部分代码,但页面无法正确加载,它给了我下面列出的错误。当我访问我的 charges#new 视图(这是我在尝试将所有内容移至我的 users#show 视图之前设置的默认视图)时,视图加载并且没有问题。但是当我把它放在部分中并尝试在我的用户#show 中使用部分时,我得到一个 NoMethodError/Undefined Method
并且我似乎无法弄清楚为什么。
下面是错误和我的代码(我在错误消息中突出显示的行上添加了注释)。如果需要添加任何其他 code/info,请告诉我。预先感谢您的帮助!
错误
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部分
<% 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 %>
用户#show
<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
最终成为愚蠢而简单的事情。我需要在我的 user_controller
中定义 @stripe_btn_data
,因为我在用户视图中使用部分。
我在我的应用程序中实现了 Stripe,但我正在尝试将我的 "Upgrade Membership" 选项移动到部分。我将代码放在一个部分中,并试图在我的 users#show 视图中使用部分代码,但页面无法正确加载,它给了我下面列出的错误。当我访问我的 charges#new 视图(这是我在尝试将所有内容移至我的 users#show 视图之前设置的默认视图)时,视图加载并且没有问题。但是当我把它放在部分中并尝试在我的用户#show 中使用部分时,我得到一个 NoMethodError/Undefined Method
并且我似乎无法弄清楚为什么。
下面是错误和我的代码(我在错误消息中突出显示的行上添加了注释)。如果需要添加任何其他 code/info,请告诉我。预先感谢您的帮助!
错误
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部分
<% 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 %>
用户#show
<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
最终成为愚蠢而简单的事情。我需要在我的 user_controller
中定义 @stripe_btn_data
,因为我在用户视图中使用部分。