Reputation: 3272
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"=>"[email protected]", "address_country"=>"France", "address_city"=>"Paris", "quantity"=>"1", "verify_sign"=>"AumOxKSV7Re473t76kESkdv3agufAX.VzyW2dEiO-ul3gPNvbfQLzqXq", "payer_email"=>"[email protected]", "txn_id"=>"8MB257669F3772042", "payment_type"=>"instant", "last_name"=>"XXXX", "address_state"=>"Alsace", "receiver_email"=>"[email protected]", "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
Upvotes: 0
Views: 1682
Reputation: 3272
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
Upvotes: 1
Reputation: 686
\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.
Upvotes: 0
Reputation: 389
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
Upvotes: 0