【问题标题】:Managing forms through another controller and staying on same page通过另一个控制器管理表单并保持在同一页面上
【发布时间】: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


【解决方案1】:

在您的场景中可能有 2 种情况-:

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.如果要将页面重定向到事件显示页面,则必须在events_path(@event) 中传递一个@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

我认为这对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 2017-11-20
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 1970-01-01
    相关资源
    最近更新 更多