我在使用我的rails应用程序设置Stripe的订阅时遵循Railscast#288。当我输入/ subscriptions / new?plan_id = monthly我收到了
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
答案 0 :(得分:1)
我认为plan_id应该是一个整数,而不是'月',所以它应该是与计划相对应的id
除非您使用gem,否则您还需要将js文件的行添加到application.html.erb文件中。
您应该查看条带文档以查看更改。
我认为有轨电视有点过时了。
答案 1 :(得分:1)
在查询字符串中,您将monthly
值传递为plan_id
(应为id)。如果要传递计划名称,可以将查询更改为:/subscriptions/new?plan=monthly
并将您的控制器更改为:
class SubscriptionsController < ApplicationController
def new
plan = Plan.find_by!(name: params[:plan])
@subscription = plan.subscriptions.build
end
...
答案 2 :(得分:0)
您尝试使用id
方法通过find
找到记录,而params[:plan_id]
看起来像name
列。您有两个选择:
发送计划ID:
?plan_id=1
更改加载计划实例的方式:
Plan.find_by!(name: 'monthly')