Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod isn't being called
    primarykey
    data
    text
    <p>I am trying to process card payments with Stripe but im struggling with the basic rails stuff, I am a novice. I cant get the controller to use the method "save_with_payment", its skips and redirect with the error notice. The form is on the events show page and I have a registrations model to handle the payment. Apologies for the incorrect indents I am still getting the hang of this. 1 other mistake is that the redirect goes to the event index page and not the show page. Very confused.</p> <h2>Model</h2> <pre><code>class Registration include Mongoid::Document field :address field :email field :job_title field :company field :first_name field :last_name field :postcode field :stripe_card_token field :stripe_customer_token def save_with_payment if valid? charge = Stripe::Charge.create({ amount: 40000, currency: "gbp", card: stripe_card_token, description: email }) save end rescue Stripe::CardError =&gt; e end end </code></pre> <h2>Controller</h2> <pre><code>class EventsController &lt; ApplicationController def index @event = Event.all end def show @event = Event.where(:slug =&gt; params[:slug]).first @registration = Registration.new end def payment @registration = Registration.new(params[:registration]) if @registration.save_with_payment(@event) redirect_to events_path(@event), :notice =&gt; "Thank you for registering for" else redirect_to events_path(@event), :notice =&gt; "We're sorry, something went wrong" end end end </code></pre> <h2>Routes</h2> <pre><code>match 'registration' =&gt; 'events#show', :via =&gt; :get match 'registration' =&gt; 'events#payment', :via =&gt; :post match '/:slug' =&gt; "events#show" </code></pre> <h2>View</h2> <pre><code>&lt;%= simple_form_for [@registration], :method =&gt; :post, :url =&gt; registration_path do |f| %&gt; &lt;%= f.input :first_name %&gt; &lt;%= f.input :last_name %&gt; &lt;%= f.input :email %&gt; &lt;%= f.input :job_title %&gt; &lt;%= f.input :company %&gt; &lt;%= f.input :stripe_card_token, as: :hidden %&gt; &lt;div class="input card_number"&gt; &lt;label for="card_number"&gt;Card number&lt;/label&gt; &lt;%= text_field_tag :card_number %&gt; &lt;/div&gt; &lt;div class="input card_cvc"&gt; &lt;label for="card_code"&gt;Security code (CVC)&lt;/label&gt; &lt;%= text_field_tag :card_cvc %&gt; &lt;/div&gt; &lt;div class="input card_dates"&gt; &lt;label&gt;Card expiration date&lt;/label&gt; &lt;%= select_month nil, { add_month_number: true }, { id: "card_month"} %&gt; &lt;%= select_year nil, { add_year: Date.today.year, end_year: Date.today.year + 15 }, { id: "card_year"} %&gt; &lt;/div&gt; &lt;%= f.button :submit, "Register", class: "button" %&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload