我的网站上有条带订阅。昨天我为用户创建了一种基本的方式来查看他们的付款记录和使用的卡。我现在想要将其分解为两个视图,以便付款历史记录在一个页面上,并且使用的卡片在另一个页面上。我觉得我必须遗漏一些东西但是当我试图这样做时,页面上没有显示任何内容。我对rails非常陌生,所以整个条带订阅的事情有点挑战但是到了那里。这是原始代码可能有人提供了一些关于我需要遵循的步骤的指导。我试着把它分成两个不同的视图文件,但是没有用,所以我回到了原来的位置!
应大力/ show.html.erb
<h1>View your payment history and cards</h1>
<h4><%= @charges.data.length %> Charges! </h4>
<% @charges.data.each do |charge| %>
<%= charge.statement_description %>
£<%= charge.amount %>
<%= Time.at(charge.created).strftime("%d/%m/%y") %>
<% end %>
<h4><%= @charges.data.length %> Cards! </h4>
<% @cards.each do |card| %>
<%= card.brand %>
**** **** **** <%= card.last4 %>
<%= card.exp_month %>/<%= card.exp_year %>
<% end %>
订阅控制器:
class SubscriptionsController < ApplicationController
def new
@subscription = Subscription.new
end
def create
# raise 'a'
@subscription = Subscription.new(params[:subscription].permit(:stripe_card_token))
@subscription.user = current_user
if @subscription.save_with_payment(params[:plan]) #(current_user)
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
def destroy
end
def show
customer_token = current_user.subscription.try(:stripe_customer_token)
@charges = customer_token ? Stripe::Charge.all(customer: customer_token) : []
@cards = customer_token ? Stripe::Customer.retrieve(customer_token).cards : []
end
end
订阅模型
class Subscription < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id #it should be always compulsary
# attr_accessible :plan
attr_accessor :stripe_card_token
def save_with_payment(plan)
if valid?
customer = Stripe::Customer.create(description: 'subscription', plan: plan, 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
答案 0 :(得分:1)
实现它的最简单方法是在控制器中创建2个方法(例如payments
和cards
):
def payments
customer_token = current_user.subscription.try(:stripe_customer_token)
@charges = customer_token ? Stripe::Charge.all(customer: customer_token) : []
end
def cards
customer_token = current_user.subscription.try(:stripe_customer_token)
@cards = customer_token ? Stripe::Customer.retrieve(customer_token).cards : []
end
为这些(payments.html.erb
目录下的cards.html.erb
和/views/subscriptions/
创建2个视图:
payments.html.erb:
<h1>View your payment history</h1>
<h4><%= @charges.data.length %> Charges! </h4>
<% @charges.data.each do |charge| %>
<%= charge.statement_description %>
£<%= charge.amount %>
<%= Time.at(charge.created).strftime("%d/%m/%y") %>
<% end %>
cards.html.erb:
<h1>View your cards</h1>
<h4><%= @charges.data.length %> Cards! </h4>
<% @cards.each do |card| %>
<%= card.brand %>
**** **** **** <%= card.last4 %>
<%= card.exp_month %>/<%= card.exp_year %>
<% end %>
并按以下方式编辑路线:
resources :subscriptions do
get '/payments', to: 'subscriptions#payments' # or get 'subscriptions/payments', to: 'subscriptions#payments' (depending on which link you would like to see)
get '/cards', to: 'subscriptions#cards'
end
这就是它。运行rake routes
以查看生成的路由以正确使用它,您就可以开始使用了。如果你要复制一些东西,请检查拼写错误 - 不确定我是否没有。