我试图了解如何将条纹支付构建到我的事件控制器中,但是我正在努力想象一切如何工作而不会说'/ registration / new'并保持用户保持不变页面即/ tech。
注意 - 我使用的是mongoid not 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几个月,所以我还在学习。
任何帮助都将不胜感激。
答案 0 :(得分:0)
在你的场景中可能有两种情况 - :
1.当您想要将用户重定向到事件索引页面时,如您的event_path所示。然后你在控制器中有错误的动作声明。在EventsController
中为索引添加一个操作
def index
end
如果你想进入事件索引页面,请按照这样的路线 -
Disrupt::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
match '/home' => "pages#home"
match '/:slug' => "events#show"
match '/events' => "events#index"
root to: "pages#home"
end
2.如果您想将页面重定向到事件展示页面,那么您必须在@event
中传递一个events_path(@event)
实例变量 - :
def payment
@registration = Registration.new(params[:registration])
if @registration.save_with_payment(@event)
redirect_to event_path(@event), :notice => "Thank you for registering"
else
redirect_to event_path(@event), :notice => "We're sorry, something went wrong"
end
end
我认为这有助于你。