0

我试图弄清楚如何将条带支付构建到我的事件控制器中,但我正在努力弄清楚一切是如何工作的,而不是说'/registration/new'并将用户保持在同一页面上,即/技术。

注意 - 我使用的是 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 几个月,所以我还在学习。

任何帮助,将不胜感激。

4

1 回答 1

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

我认为这对你有帮助。

于 2013-07-08T13:28:25.523 回答