Note that there are some explanatory texts on larger screens.

plurals
  1. POStripe payments, adding price from another model into payment method
    text
    copied!<p>I have added Stripe payment to my app to handle registration payments for events, the form is on the event show page so the method is in the events controller but I have created a registration model. My issue is trying to get the price of the event into the <code>save_with_payment</code> method inside the registration model(<code>total_price</code>).</p> <p>Any ideas? I have tried <code>attr_accessible :price</code> but with no luck.</p> <h2>Event Controller</h2> <pre><code>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 RegistrationMailer.admin(@registration, @event).deliver RegistrationMailer.delegate(@registration, @event).deliver redirect_to root_path flash[:success] = "Thank you for registering for &lt;% @event.title %&gt;" else redirect_to root_path flash[:error] = "We're sorry, something went wrong" end end </code></pre> <h2>Registration Model</h2> <pre><code>def save_with_payment if valid? charge = Stripe::Charge.create({ amount: total_price, ##I can declare a integer here instead of a method and it works currency: "gbp", card: stripe_card_token, description: email }) save end rescue Stripe::CardError =&gt; e end def total_price @event.price = price price end </code></pre> <h2>Routes</h2> <pre><code>match 'registration' =&gt; 'events#show', :via =&gt; :get match 'registration' =&gt; 'events#payment', :via =&gt; :post </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; </code></pre>
 

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