2014-02-20 30 views
0

我在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 

回答

1

在您的查詢字符串中,您傳遞的monthly值爲plan_id(應該是id)。如果你想通過計劃的名稱,你可以改變你查詢到看起來像:使用通過id

class SubscriptionsController < ApplicationController 
    def new 
    plan = Plan.find_by!(name: params[:plan]) 
    @subscription = plan.subscriptions.build 
    end 
... 
+0

我以前使用過一個id,例如/ new?plan_id = 1,它給出了同樣的錯誤「找不到計劃id = 1」 –

+1

也許你的數據庫中沒有id = 1的計劃? – ivalkeen

1

我相信plan_id的數據類型應該是一個整數,而不是 '月月',所以它應該是與計劃對應的ID

您還需要將js文件的行添加到application.html.erb文件中unl你使用寶石。

您應該看看條紋文檔以查看更改。

我相信railscast有點過時了。

+0

由於將採取深入研究文檔。我必須將計劃ID信息添加到計劃表中。謝謝! –

0

你試圖查找記錄:/subscriptions/new?plan=monthly

,改變你的控制器看起來像find方法和您的params[:plan_id]看起來像name列。你有兩個選擇:

  • 發送計劃ID:

    ?plan_id=1

  • 更改您加載計劃實例的方式:

    Plan.find_by!(name: 'monthly')

+0

謝謝。我完全忘了先把計劃信息添加到計劃表中。它現在作爲id = 1工作。 –