我在Railscast#288中使用我的Rails應用程序設置Stripe的訂閱。當我輸入/訂閱/新?plan_id的數據類型=月我收到一個找不到計劃id =每月
ActiveRecord::RecordNotFound error Couldn't find Plan with id=monthly.
我注視着將文件添加到application.html.erb之外的一切。我無法在條紋文檔中找到這些信息,所以我認爲教程的一部分已經過時(加上那與我當前的錯誤無關)。
控制器:
class SubscriptionsController < ApplicationController
def new
plan = Plan.find(params[:plan_id])
@subscription = plan.subscriptions.build
end
def create
@subscription = Subscription.new(params[:subscription])
if @subscription.save_with_payment
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
def show
@subscription = Subscription.find(params[:id])
end
end
控制器:
class PlansController < ApplicationController
def index
@plans = Plan.order("price")
end
end
途徑:
resources :subscriptions
resources :plans
型號:
class Plan < ActiveRecord::Base
has_many :subscriptions
end
型號:
class Subscription < ActiveRecord::Base
belongs_to :plan
validates_presence_of :plan_id
validates_presence_of :email
attr_accessor :stripe_card_token
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
new.html.erb:
<h1>Signing up for "<%= @subscription.plan.name %>"</h1>
<p>Includes <strong><%= @subscription.plan.length %> Subscription</strong> for only <strong><%= number_to_currency @subscription.plan.price %></strong> per month!</p>
<%= form_for @subscription do |f| %>
<% if @subscription.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(@subscription.errors.count, "error") %> prohibited this subscription from being saved:</h2>
<ul>
<% @subscription.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.hidden_field :plan_id %>
<%= f.hidden_field :stripe_card_token %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<% if @subscription.stripe_card_token.present? %>
Credit card has been provided.
<% else %>
<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
</div>
<% end %>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<div class="actions">
<%= f.submit "Subscribe" %>
</div>
<% end %>
schema.rb:
create_table "subscriptions", force: true do |t|
t.integer "plan_id"
t.string "email"
t.string "stripe_customer_token"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "plans", force: true do |t|
t.string "name"
t.decimal "price", precision: 10, scale: 0
t.integer "length"
t.datetime "created_at"
t.datetime "updated_at"
end
我以前使用過一個id,例如/ new?plan_id = 1,它給出了同樣的錯誤「找不到計劃id = 1」 –
也許你的數據庫中沒有id = 1的計劃? – ivalkeen