0

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

ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67

3 Answers3

1

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
ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67
0

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
Pavel S
  • 389
  • 1
  • 9
  • Thank you @pavelS, yes I tried your proposal but I get the error `ArgumentError (invalid byte sequence in UTF-8)` – ZazOufUmI Oct 27 '14 at 22:16
  • @ZazOufUmI please read this http://stackoverflow.com/questions/11375342/stringencode-not-fixing-invalid-byte-sequence-in-utf-8-error `first_name` and `address` params causing the issue. Fixing your encoding might fix the problem with `serialize` – Pavel S Oct 27 '14 at 22:32
  • I agreed with you about `first_name` params, but I cannot control those params (came from user). I tried `.encode!` but it returns `NoMethodError (undefined method encode!' for #)` . Same for `Serialize` returns `NoMethodError (undefined method serialize' for #)` – ZazOufUmI Oct 27 '14 at 22:47
  • Use `params.map { |x| x.try(:scrub) }` to remove all invalid chars in strings. It's built in in 2.1, there is also gem for earlier versions.To accept nested hashes you should implement `deep_map` (you can google for it, here is one option https://www.ruby-forum.com/topic/218112). P.S. `serialize` should be used in the model, not in the controller. – prcu Oct 28 '14 at 05:36
0

\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.

Community
  • 1
  • 1
Prashant Kumar
  • 678
  • 6
  • 7