【发布时间】:2013-07-05 15:10:37
【问题描述】:
我正试图弄清楚如何在我的事件控制器中构建条带支付,但我正在努力弄清楚一切是如何工作的,而不会说“/registration/new”并让用户保持不变页面即 /tech。
注意 - 我使用的是 mongoid 而不是 activerecord
到目前为止,这是我的代码:
型号
class Registration
include Mongoid::Document
field :address
field :email
field :first_name
field :last_name
field :postcode
field :stripe_card_token
field :stripe_customer_token
def save_with_payment event
if valid?
charge = Stripe::Charge.create({
amount: total_price(event),
currency: "gbp",
card: stripe_card_token,
description: email
})
save
end
rescue
false
end
def total_price attribute
price = attribute.price
price
end
end
表格
<%= simple_form_for @registration, :html => { :class => 'custom medium' },
:method => :post, :url => registration_path do |f| %>
<h2>Contact Information</h2>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :address %>
<%= f.input :postcode %>
<!-- Add stripe card details -->
<h2>Payment Information</h2>
<%= f.input :stripe_card_token, as: :hidden %>
<div class="input card_number">
<label for="card_number">Card number</label>
<%= text_field_tag :card_number %>
</div>
<div class="input card_cvc">
<label for="card_code">Security code (CVC)</label>
<%= text_field_tag :card_cvc %>
</div>
<div class="input card_dates">
<label>Card expiration date</label>
<%= select_month nil, { add_month_number: true }, { id: "card_month"} %>
<%= select_year nil,
{ add_year: Date.today.year, end_year: Date.today.year + 15 },
{ id: "card_year"} %>
</div>
<%= f.button :submit, "Register" %>
<% end %>
控制器
class EventsController < ApplicationController
def show
@event = Event.where(:slug => params[:slug]).first
@registration = Registration.new
end
def payment
@registration = Registration.new(params[:registration])
if @registration.save_with_payment(@event)
redirect_to event_path, :notice => "Thank you for registering"
else
redirect_to event_path, :notice => "We're sorry, something went wrong"
end
end
end
路线
Disrupt::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
root to: "pages#home"
match '/home' => "pages#home"
match '/:slug' => "events#show"
end
目前页面重定向到主页,并没有回到事件页面显示通知,我只使用rails几个月,所以我还在学习。
任何帮助将不胜感激。
【问题讨论】:
-
您没有在 EventsController 中指定索引操作。
def index end。我认为这有帮助。或者,如果您提供路线可能会更有帮助。 -
我没有索引页或控制器。我已经按照你的建议添加了路线。
标签: ruby-on-rails ruby ruby-on-rails-3.2 mongoid stripe-payments