我已将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" %>
答案 0 :(得分:1)
如果我把一切都搞定了,请点击这里:
def show
@event = Event.where(:slug => params[:slug]).first
@registration = Registration.new
end
是带有视图的控制器操作,包含表单。
我认为,通过:has_one
然后您的注册模型需要event_id
整数列。当您在节目中声明@event
时,请在
<%= f.input :event_id, as: :hidden, value: @event.id %>
之后,您可以通过以下方式在付款方式中找到@event
@event=Event.find(params[:registration][params[:event_id]].to_i)
这是一个基本的例子,我实际上并不知道,您的应用中的事件是什么,可能有更好的属性,您可以通过。
之后,您可以将@event.price
作为参数传递给模型方法:
def save_with_payment(total_price)
if valid?
charge = Stripe::Charge.create({
amount: total_price,
#other code