我已将 Stripe 付款添加到我的应用程序以处理事件的注册付款,表单位于事件显示页面上,因此该方法位于事件控制器中,但我创建了一个注册模型。我的问题是试图将事件的价格放入save_with_payment
注册模型(total_price
)中的方法中。
有任何想法吗?我试过attr_accessible :price
但没有运气。
事件控制器
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
RegistrationMailer.admin(@registration, @event).deliver
RegistrationMailer.delegate(@registration, @event).deliver
redirect_to root_path
flash[:success] = "Thank you for registering for <% @event.title %>"
else
redirect_to root_path
flash[:error] = "We're sorry, something went wrong"
end
end
注册模式
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 => e
end
def total_price
@event.price = price
price
end
路线
match 'registration' => 'events#show', :via => :get
match 'registration' => 'events#payment', :via => :post
看法
<%= simple_form_for [@registration], :method => :post, :url => registration_path do |f| %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email %>
<%= f.input :job_title %>
<%= f.input :company %>
<%= 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", class: "button" %>