【问题标题】:Retrieve Incoming Parameters in Rails4在 Rails4 中检索传入参数
【发布时间】:2014-06-02 10:48:23
【问题描述】:

我将一些参数发布到我的 Rails4 应用程序中的远程 URL,来自 /orders/:id 路由和 show controller 方法。

从我的应用程序发送到服务的参数,该服务根据响应是否成功将一堆参数返回到我的应用程序的 /fail 或 /success 页面。我可以从日志中看到传入的参数。

我想在我的 Order 控制器中使用此参数创建一个 OrderTransaction,我该怎么做?

orders_controller.rb

  def success
    ### I would like to create an OrderTransaction here with the incoming parameters!
    @transaction = OrderTransaction.new(????????)
  end

  def fail
    ### I would like to create an OrderTransaction here with the incoming parameters!
    @transaction = OrderTransaction.new(????????)
  end

日志中的传入参数 =>

Started POST "/orders/fail" for 127.0.0.1 at 2014-06-01 13:40:28 +0300
Processing by OrdersController#fail as HTML
Parameters: 
  {
    "TRANID"=>"", 
    "type"=>"Auth", 
    "refreshtime"=>"5", 
    "lang"=>"tr", 
    "amount"=>"30", 
    "ACQBIN"=>"490740", 
    "clientIp"=>"193.140.28.145", 
    "name"=>"AKBANK", 
    "cardHolderName"=>"dsadas dasdsa", 
    "okUrl"=>"http://localhost:3000/orders/success",
    "failUrl"=>"http://localhost:3000/orders/fail",
    "storetype"=>"3d_pay_hosting",
    "Response"=>"Declined"
    ....
  }

order.rb =>

class Order < ActiveRecord::Base
  belongs_to :user
  belongs_to :participation
  has_one :transaction, :class_name => "OrderTransaction"
end

order_transaction.rb =>

class OrderTransaction < ActiveRecord::Base
  belongs_to :order
end

routes.rb =>

post 'orders/success' => 'orders#success'
post 'orders/fail' => 'orders#fail'
resources :orders, only: [:index, :new, :create, :show]

orders_controller.rb =>

require 'digest/sha1'

class OrdersController < ApplicationController
  before_filter :authenticate_user!
  before_action :set_order, only: [:show, :edit, :update, :destroy]
  skip_before_action :verify_authenticity_token

  def index
    @orders = Order.all
  end

  def show
    @hashing = {
      clientid: POS['clientid'],
      oid: Time.now.to_i.to_s,
      amount: @order.participation.examination.exam_fees.first.fee.to_s,
      okUrl: POS['okUrl'],
      failUrl: POS['failUrl'],
      islemtipi:POS['islemtipi'],
      taksit: '',
      rnd: Time.now.to_i.to_s,
    }
  end

  # GET /orders/new
  def new
    @order = Order.new
  end

  def create
    @order = Order.new(order_params)
    @order.user_id = current_user.id
    respond_to do |format|
      if @order.save
        ...
      else
        ...
      end
    end
  end

  def success
    ### I would like to create an OrderTransaction here with the incoming parameters!
    @transaction = OrderTransaction.new(????????)
  end

  def fail
    ### I would like to create an OrderTransaction here with the incoming parameters!
    @transaction = OrderTransaction.new(????????)
  end

  private

  def set_order
    @order = Order.find(params[:id])
  end

  def order_params
    params.require(:order).permit(:id, :user_id, :participation_id, :amount, :cardHolderName)
  end
end

orders/show.html.erb=>

<%= simple_form_for(@order, html:{class: "well"}, :url => "https://testsanalpos.est.com.tr/servlet/est3Dgate", :method => :post) do |f| %>
  <% @hashing.each do |k, v| %>
    <%= f.input k, input_html: {name: k, value: v}, as: :hidden %>
  <% end %>
  <%= f.button :submit, "Approve" %>
<% end %>

【问题讨论】:

  • 您将能够像在任何其他 Rails 请求中一样从 params 哈希中获取它们。 OrderTransaction.new(amount: params[:amount])e.t.c.

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4


【解决方案1】:

你可以这样做:

#app/controllers/orders_controller.rb
def success
   Orders.new(order_params)
end

private

def order_params
    params.permit(:client_ip, :etc, :etc)
end

--

这将使用 Rails 中传统的 strong_params 模式,它只会将您需要的参数列入白名单。考虑到您的 params hash 填充了来自远程服务的数据,这应该可以正常工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 1970-01-01
    • 2012-01-13
    相关资源
    最近更新 更多