我有一个用户支付费用的页面,并且在提交时会被重定向到显示付款完成的同一页面。我正在重定向传递id,但它丢失了导致错误。
以下是控制器操作:
def pay_all_fees
@target_action='pay_all_fees'
@target_controller='parent_wise_fee_payments'
if params[:id].present?
id = params[:id]
else id = 4
end
@students=Student.find_all_by_sibling_id(Guardian.find(id).ward_id)
@guardian = Guardian.find(id)
@students.each do |student|
fetch_all_fees
end
if request.post?
status=true
MultiFeesTransaction.transaction do
multi_fees_transaction= MultiFeesTransaction.create(params[:multi_fees_transaction])
begin
finance_transactions=FinanceTransaction.create!(params[:transactions].values)
rescue Exception => e
status=false
end
if status and (multi_fees_transaction.valid? && finance_transactions.all?(&:valid?))
multi_fees_transaction.finance_transactions=finance_transactions
flash[:notice]="#{t('fees_paid')}"
else
flash[:notice]="#{t('fee_payment_failed')}"
raise ActiveRecord::Rollback
end
end
redirect_to :controller => 'parent_wise_fee_payments', :action => 'pay_all_fees', :id=>id
end
get_paid_fees
end
这是生产日志中的错误:
Processing ParentWiseFeePaymentsController#pay_all_fees (for 178.135.80.106 at 2015-08-03 13:11:29) [POST]
Parameters: {"multi_fees_transaction"=>{"student_id"=>"4", "payment_mode"=>"Cash", "payment_note"=>"", "amount"=>"500.00", "transaction_date"=>"2015-08-03"}, "controller"=>"parent_wise_fee_payments", "transactions"=>{"4"=>{"payee_type"=>"Student", "title"=>"Receipt No.. (Multiple Fees) F3", "payment_mode"=>"Cash", "finance_id"=>"3", "category_id"=>"14", "payment_note"=>"", "payee_id"=>"4", "amount"=>"", "finance_type"=>"FinanceFee", "transaction_date"=>"2015-08-03"}, "2"=>{"payee_type"=>"Student", "title"=>"Receipt No.. (Multiple Fees) F3", "payment_mode"=>"Cash", "finance_id"=>"3", "category_id"=>"14", "payment_note"=>"", "payee_id"=>"1", "amount"=>"500", "finance_type"=>"FinanceFee", "transaction_date"=>"2015-08-03"}}, "action"=>"pay_all_fees", "authenticity_token"=>"ns37sH/tD/0i5PyMLGfG6bzEb2SHgjwJQGMuV3i3cNE=", "transaction_date"=>"2015-08-03"}
Username : admin Role : Admin
Rendering template within layouts/application
Rendering parent_wise_fee_payments/pay_all_fees
ActionView::TemplateError (undefined method `full_name' for nil:NilClass) on line #31 of vendor/plugins/acme_parent_wise_fee_payment/app/views/parent_wise_fee_payments/pay_all_fees.html.erb:
28: <div class="val themed_text">
29: <span>:</span>
30: <div class="val-align">
31: <%= @guardian.full_name %>
32: </div>
33: </div>
34:
重定向后id为null。您可以从生产日志中注意到操作“pay_all_fees”后缺少ID。你能帮帮我解决这个问题吗?有没有办法强制传递带有真实性令牌的id?例如?
更新:
在进入此页面之前,用户会搜索监护人,因此我将ID从搜索页面传递到当前页面。现在我在重定向后需要相同的id,我只是不知道如何。
答案 0 :(得分:2)
试试这个:
if request.post?
student_id=params[:multi_fees_transaction]["student_id"]
..........
redirect_to :controller => 'parent_wise_fee_payments', :action => 'pay_all_fees', :student_id=>student_id
通过这种方式,您可以使用student_id获取ID或其他内容。
答案 1 :(得分:0)
中的id变量
redirect_to :controller => 'parent_wise_fee_payments', :action => 'pay_all_fees', :id=>id
请求发布时无法访问id。在if request.post中分配id变量?块
if request.post?
if params[:id].present?
id = params[:id]
end
the rest of your code
end