イベントの登録支払いを処理するためにアプリにストライプ支払いを追加しました。フォームはイベントショーページにあるため、メソッドはイベントコントローラーにありますが、登録モデルを作成しました。私の問題は、イベントの価格を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" %>