如何将 select_tag 传递给 Rails 中的模型验证器?
How to pass select_tag to model validator in Rails?
我想用我的 cash_in_order 模型处理 Stripe 错误。
> CashInOrder.new
=> #<CashInOrder:0x000055e51bee03d0
id: nil,
deposit_id: nil,
order_sum: nil,
stripe_charge_id: nil,
created_at: nil,
updated_at: nil>
我附上 cash_in_order 表格 select_tag 以选择支付卡
<%= form_with model: @cash_in_order, class: 'form', id: 'cash_in_order' do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= select_tag 'cash_in_order[order_source]', options_for_select(cards.collect{ |c| ["**** **** **** #{c.last4}", c.id] }, cards.first.id), class: "form-control" %>
<br>
<%= f.text_field :order_sum, class: "form-control", placeholder: "Amount" %>
<br>
<%= f.submit "add founds", class: "btn btn-default" %>
<% end %>
并添加到 cash_in_order_params order_source 属性
class CashInOrdersController < ApplicationController
create
@user = current_user
@deposit = @user.deposit
@cash_in_order = @deposit.cash_in_orders.build
if current_user and current_user.deposit.id == @deposit.id
@cash_in_order.save
end
respond_to do |format|
if @cash_in_order.save
format.html { redirect_to @deposit, notice: 'Founds were added to deposit!!' }
format.json {head :no_content}
format.js { flash.now[:notice] = "Founds were added to deposit!" }
else
format.html { redirect_to @deposit, notice: 'Founds were not added to deposit! Something was going wrong' }
format.json { render json: @cash_in_order.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def cash_in_order_params
params.require(:cash_in_order).permit(:order_source, :order_sum)
end
end
表单提交后我在控制台看到
Started POST "/cash_in_orders" for ::1 at 2020-09-29 07:46:11 -0400
Processing by CashInOrdersController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Qgznx99ZUDwBG4R7moEE1gIP9o7FCpzbQh+FO8ccosrfMy5V7O5ydg/b6QvT7jZWUmQKtz+JDRO5rM4msDhzrw==", "cash_in_order"=>{"order_source"=>"card_1HSnL8KcFr6ZSgIRyUZUQrtE", "order_sum"=>""}, "commit"=>"add founds"}
order_sum params 我留空是为了从 Stripe 获取错误(大于零)。
为了处理 Stripe 错误,我在 cash_in_order 模型中创建了验证器
class CashInOrder < ApplicationRecord
validate :is_cashed_in
private
def is_cashed_in
card = self.deposit.user.retrieve_card(self.order_source) #Here is my error!!!
Stripe::Charge.create(:amount => (amount.to_f*100).to_i, :currency => 'usd', :source => card.id, :customer => self.deposit.user.stripe_customer_id)
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
我遇到错误
CashInOrdersController#create 中的 NoMethodError
#CashInOrder:0x00007f42f8f129e8
的未定义方法“order_source”
当然,我的模型没有 order_source 属性,但我将它添加到 cash_in_order_params 并且我可以在对我的服务器的请求中看到它
所以我的问题是
我怎样才能正确地将这个 order_source 参数发送到模型验证器?
感谢任何预付款!
尝试使用 attr_accessor
class CashInOrder < ApplicationRecord
attr_accessor :order_source
end
我想用我的 cash_in_order 模型处理 Stripe 错误。
> CashInOrder.new
=> #<CashInOrder:0x000055e51bee03d0
id: nil,
deposit_id: nil,
order_sum: nil,
stripe_charge_id: nil,
created_at: nil,
updated_at: nil>
我附上 cash_in_order 表格 select_tag 以选择支付卡
<%= form_with model: @cash_in_order, class: 'form', id: 'cash_in_order' do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= select_tag 'cash_in_order[order_source]', options_for_select(cards.collect{ |c| ["**** **** **** #{c.last4}", c.id] }, cards.first.id), class: "form-control" %>
<br>
<%= f.text_field :order_sum, class: "form-control", placeholder: "Amount" %>
<br>
<%= f.submit "add founds", class: "btn btn-default" %>
<% end %>
并添加到 cash_in_order_params order_source 属性
class CashInOrdersController < ApplicationController
create
@user = current_user
@deposit = @user.deposit
@cash_in_order = @deposit.cash_in_orders.build
if current_user and current_user.deposit.id == @deposit.id
@cash_in_order.save
end
respond_to do |format|
if @cash_in_order.save
format.html { redirect_to @deposit, notice: 'Founds were added to deposit!!' }
format.json {head :no_content}
format.js { flash.now[:notice] = "Founds were added to deposit!" }
else
format.html { redirect_to @deposit, notice: 'Founds were not added to deposit! Something was going wrong' }
format.json { render json: @cash_in_order.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def cash_in_order_params
params.require(:cash_in_order).permit(:order_source, :order_sum)
end
end
表单提交后我在控制台看到
Started POST "/cash_in_orders" for ::1 at 2020-09-29 07:46:11 -0400
Processing by CashInOrdersController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Qgznx99ZUDwBG4R7moEE1gIP9o7FCpzbQh+FO8ccosrfMy5V7O5ydg/b6QvT7jZWUmQKtz+JDRO5rM4msDhzrw==", "cash_in_order"=>{"order_source"=>"card_1HSnL8KcFr6ZSgIRyUZUQrtE", "order_sum"=>""}, "commit"=>"add founds"}
order_sum params 我留空是为了从 Stripe 获取错误(大于零)。
为了处理 Stripe 错误,我在 cash_in_order 模型中创建了验证器
class CashInOrder < ApplicationRecord
validate :is_cashed_in
private
def is_cashed_in
card = self.deposit.user.retrieve_card(self.order_source) #Here is my error!!!
Stripe::Charge.create(:amount => (amount.to_f*100).to_i, :currency => 'usd', :source => card.id, :customer => self.deposit.user.stripe_customer_id)
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
我遇到错误
CashInOrdersController#create 中的 NoMethodError
#CashInOrder:0x00007f42f8f129e8
的未定义方法“order_source”当然,我的模型没有 order_source 属性,但我将它添加到 cash_in_order_params 并且我可以在对我的服务器的请求中看到它
所以我的问题是 我怎样才能正确地将这个 order_source 参数发送到模型验证器?
感谢任何预付款!
尝试使用 attr_accessor
class CashInOrder < ApplicationRecord
attr_accessor :order_source
end