使用带设计的条纹?

Posted

技术标签:

【中文标题】使用带设计的条纹?【英文标题】:Using Stripe with Devise? 【发布时间】:2012-03-26 13:37:19 【问题描述】:

我是一名 RoR 新手,正在尝试将 Stripe 与 Devise 集成,以便我可以注册我的应用的新订阅者,并将他们设置为按月收费。我已关注 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>

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

models/user.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
...

views/users/registrations/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" %>

assets/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)

【问题讨论】:

你有没有想过这个问题?我遇到了同样的问题。 不,我最终使用了 SaaS RailsKit,它现在包含 Stripe 并拥有 Devise。 以下解决方案应该可以为您节省一些现金;) 【参考方案1】:

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

像这样--

controllers/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

【讨论】:

以上是关于使用带设计的条纹?的主要内容,如果未能解决你的问题,请参考以下文章

Drupal-带条纹的主题表

条纹编码很奇怪

《CSS揭秘》读书笔记-条纹背景

[css 揭秘] :CSS揭秘 技巧:条纹背景

如何去除使用线性渐变属性时出现的条纹[重复]

如何去除使用线性渐变属性时出现的条纹[重复]