我的控制器订阅有问题。 我尝试使用Stripe创建表单进行付款。 我在seeds.rb上创建了3个计划,我可以使用ID索引并显示它们,但我不知道如何将plans / show的id转换为subscription / new。
这是我的subscription_controller.rb:
require "stripe"
skip_before_filter :verify_authenticity_token
def new
@titre = "Abonnement"
@subscription = Subscription.new params[:subscription]
@plan = Plan.find params[:subscription][:plan_id]
if @subscription.save_with_payment
flash[:success] = 'Félicitations ! Vous êtes maintenant abonné à notre service.'
redirect_to root_path
else
flash.now[:error] = "Erreur lors de la crétion de votre formule d'abonnement"
render template: 'plans/show'
end
end
def create
@subscription = Subscription.new(subscription_params)
end
private
def subscription_params
params.require(:registration).permit(:lastname, :email, :firstname, :company, :city, :country, :adress, :postalcode)
end
这是我的plan_controller.rb:
require "stripe"
before_action :set_plan, only: [:show]
skip_before_filter :verify_authenticity_token
def index
@plans = Plan.all
end
def show
end
我的订阅模式:
belongs_to :plan
belongs_to :user
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
self.next_bill_on = Date.parse customer.next_recurring_charge.date
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Erreur Stripe lors de la création du profil client : #{e.message}"
errors.add :base, "Il y a eu un problème lors du paiement avec votre carte."
false
end
和我的注册/ new.html.erb:
<h1>Signing up for "<%= @registration.plan.name %>"</h1>
<h2><%= number_to_currency @registration.plan.price %></h2>
<%= form_for @registration do |f| %>
<% if @registration.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(@registration.errors.count, "error") %> prohibited this subscription from being saved:</h2>
<ul>
<% @registration.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 @registration.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 %>
如果有人能给我一个跟踪他的话,我将非常感激:)
由于