我對Rails非常陌生,如果這個問題很簡單,但我已經瀏覽了其他SO帖子和其他網站上沒有運氣的區域。NoMethodError /未定義的方法,雖然試圖部分使用條紋
我實現了條紋到我的應用程序,但我試圖將我的「升級成員」選項移動到一個局部。我把代碼放入局部區域並嘗試在我的用戶中使用局部區域#show view和當我訪問我的收費#新視圖(這是我在嘗試將所有內容移動到用戶#show view之前設置的默認視圖)時,視圖加載並沒有問題,但是當我把它扔在部分,並嘗試使用我的用戶#局部顯示,我得到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部分
<% 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>
我有另外一個PA rtial,我使用的部分內,我有一個錯誤,不知道它是否有關,但在這裏,以防萬一。
降級偏
<%= 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