简体   繁体   中英

Ruby on Rails - Store params to database

I have this Hash params from a POST request (it is an 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. 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)

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.

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

Did you tried to use 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

\\xE9 in St\\xE9phane is a unicode character for U+00E9 . You need to replace it with '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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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