Ruby on Rails - 将params存储到数据库

时间:2014-10-27 22:02:47

标签: ruby-on-rails ruby hash parameters

我有来自POST请求的这个Hash参数(它是ActionController::Parameters):

{"mc_gross"=>"1.00", "invoice"=>"28", "protection_eligibility"=>"Eligible", "address_status"=>"unconfirmed", "payer_id"=>"FSXBUQDGG6KWN", "tax"=>"0.00", "address_street"=>"Av. de la Pelouse, 87648672 Mayet", "payment_date"=>"14:46:27 Oct 27, 2014 PDT", "payment_status"=>"Completed", "charset"=>"windows-1252", "address_zip"=>"75002", "first_name"=>"St\xE9phane", "mc_fee"=>"0.34", "address_country_code"=>"FR", "address_name"=>"St\xE9phane XXX", "notify_version"=>"3.8", "custom"=>"", "payer_status"=>"verified", "business"=>"stephaneXXXX@gmail.com", "address_country"=>"France", "address_city"=>"Paris", "quantity"=>"1", "verify_sign"=>"AumOxKSV7Re473t76kESkdv3agufAX.VzyW2dEiO-ul3gPNvbfQLzqXq", "payer_email"=>"zXXXXXXX@k.com", "txn_id"=>"8MB257669F3772042", "payment_type"=>"instant", "last_name"=>"XXXX", "address_state"=>"Alsace", "receiver_email"=>"stephaneXXXXXX@gmail.com", "payment_fee"=>"0.34", "receiver_id"=>"ZNER97N82WKY2", "txn_type"=>"web_accept", "item_name"=>"XXXX", "mc_currency"=>"USD", "item_number"=>"1", "residence_country"=>"FR", "test_ipn"=>"1", "handling_amount"=>"0.00", "transaction_subject"=>"", "payment_gross"=>"1.00", "shipping"=>"0.00", "ipn_track_id"=>"5db890c138b56", "controller"=>"purchases", "action"=>"hook"}

我想将它保存在我的数据库中。这样做的最佳方式是什么?

我尝试将它转换为带有params.inspect的String,但它不起作用,我也尝试序列化(params)但也失败了。 我的专栏是t.text,当我@purchase.update_attributes my_column: params原样时,我收到错误:ArgumentError (invalid byte sequence in UTF-8)

由于

3 个答案:

答案 0 :(得分:1)

经过多次尝试但没有成功,我决定绕过这个问题。 由于数据库字段是一个文本列,我所做的是创建一个包含所有参数的字符串。之后,我能够将我的String存储到我的文本字段数据库列中。

parameters = String.new
params.each do |key, value|
  parameters += key + " = " + value + " | "
end

答案 1 :(得分:0)

您是否尝试使用rails serialize

#in your model
class Purchase < ActiveRecord::Base
  serialize :my_column
end

#in controller
def myaction
  #...
  @purchase.my_column = my_params
  @purchase.save
end

答案 2 :(得分:0)

St \ xE9phane中的

\ xE9是U+00E9的unicode字符。您需要将其替换为“e”。

2.1.0 :010 > a="St\xE9phane"
=> "St\xE9phane" 
2.1.0 :011 > a.scrub!('e')
=> "Stephane"

或者您可以将数据库配置为使用unicode store unicode in mysql