简体   繁体   English

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

[英]Ruby on Rails - Store params to database

I have this Hash params from a POST request (it is an ActionController::Parameters ) : 我有一个来自POST请求的哈希参数(这是一个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"}

I want to save it in my database. 我想将其保存在数据库中。 What is the best way to do that ? 最好的方法是什么?

I tried converting it to a String with params.inspect but it doesn't work, I also tried serialize(params) but failed too. 我尝试使用params.inspect将其转换为String,但是它不起作用,我也尝试了serialize(params)但也失败了。 My column is a t.text, when I do @purchase.update_attributes my_column: params as it is, I get the error : ArgumentError (invalid byte sequence in UTF-8) 我的列是t.text,当我执行@purchase.update_attributes my_column: params时,出现错误: ArgumentError (invalid byte sequence in UTF-8)

Thanks 谢谢

After multiple tries without any success I decided to bypass the issue. 经过多次尝试均未成功,我决定绕过此问题。 Since by database field is a text column, what I did is to create a String of all my parameters. 由于按数据库字段是一个文本列,因此我要做的是创建一个包含所有参数的字符串。 After that, I was able to store my String into my text field database column. 之后,我能够将String存储到我的文本字段数据库列中。

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

Did you tried to use rails serialize ? 您是否尝试过使用Rails序列化

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

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

\\xE9 in St\\xE9phane is a unicode character for U+00E9 . St \\ xE9phane中的\\ xE9是U + 00E9的Unicode字符。 You need to replace it with 'e'. 您需要将其替换为“ e”。

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

or you can configure your database to use unicode store unicode in mysql . 或者您可以配置数据库以在mysql中使用unicode 存储unicode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM