2

我是一名尝试将 Stripe 与 Devise 集成的 RoR 新手,以便我可以注册我的应用程序的新订阅者,并将其设置为按月收费。我已经关注了 Railscast ( http://railscasts.com/episodes/288-billing-with-stripe ) 并查看了这里的 QA ( Using Stripe with Devise for Ruby on Rails Subscriptions ),但仍然不能让它工作/完全理解我做错了什么。本质上,我希望(如果可能)使用我的设计用户模型和注册控制器,而不必创建/使用订阅模型/控制器。

我将 Railscast 中的代码放在适当的位置,但仍然收到错误,例如:

NoMethodError in Users/registrations#create

Showing /app/views/users/registrations/new.html.erb where line #7 raised:

undefined method `errors' for nil:NilClass

Extracted source (around line #7):

4: <h2>Sign up</h2>
5: 
6: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
7:   <%= devise_error_messages! %>
8: 
9:   <div><%= f.label :email %><br />
10:   <%= f.email_field :email %></div>

非常感谢任何帮助/指导。

模型/用户.rb

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,:invitable

       has_many :org_roles 
      has_many :orgs, :through => :org_roles, :conditions => "role_id = 1"
      accepts_nested_attributes_for :orgs, :allow_destroy => true
      has_many :assigned_orgs, :through => :org_roles, :conditions => "role_id = 2" , :class_name => "Org", :source => :org
      accepts_nested_attributes_for :assigned_orgs
      has_one :user_detail
      accepts_nested_attributes_for :user_detail, :allow_destroy => true
      has_one :user_address
      accepts_nested_attributes_for :user_address, :allow_destroy => true

      has_many :subscription_plans

      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me, :user_type_id, :orgs_attributes, :assigned_orgs_attributes, :user_detail_attributes, :user_address_attributes

      #STRIPE 
      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

控制器/registrations_controller.rb

def create

  @subscription = User.new(params[:subscription])
  if @subscription.save_with_payment
    redirect_to @subscription, :notice => "Thank you for subscribing!"
  else
    render :new
  end

   super
...

意见/用户/注册/new.html.erb

<%= javascript_include_tag "https://js.stripe.com/v1/", "application" %>
<%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div>Your Business Name:<br />
  <%= text_field_tag :org %></div>

<div>Please select a subscription plan: <br />
        <%= radio_button_tag(:subscription_plan, 1, :checked=>"yes") %>
        <%= label_tag(:subscription_plan_tag, "Small") %>
        <%= radio_button_tag(:subscription_plan, 2) %>
        <%= label_tag(:subscription_plan_tag, "Medium") %>
        <%= radio_button_tag(:subscription_plan, 3) %>
        <%= label_tag(:subscription_plan_tag, "Large") %>
  </div>

<%= f.hidden_field :stripe_card_token %>

  <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>
<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><%= f.submit "Sign up" %></div>
<% end %>

<%= render "links" %>

资产/javascripts/users/users.js.coffee

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  subscription.setupForm()

subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, subscription.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#subscription_stripe_card_token').val(response.id)
      $('#new_user')[0].submit()
      #alert(response.id)
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)
      #alert(response.error.message)
4

1 回答 1

4

根据您命名模型(用户)的方式,您应该更改@subscription -> @user。

像这样 -

控制器/registrations_controller.rb

def create

  @user = User.new(params[:user])
  if @user.save_with_payment
    redirect_to @user, :notice => "Thank you for subscribing!"
  else
    render :new
  end

super
于 2012-06-14T22:45:52.357 回答