Post json with JavaScript to Rails - undefined method stringify keys -
i trying post javascript object rails api in order create model object on server.
here how define javascript object (coffee script) :
createlist: (list) -> params = list:list $$.post 'http://localhost:3000/lists.json', params, ((response) -> ), 'json'
so function parameter contains list want save.
in rails, create list :
@list = list.create name:params[:list] if @list.save head :ok else respond_with error:'error while creating list.' end
i following error : undefined method `stringify_keys' "[object object]":string
this how params
variable set in rails :
parameters: {"list"=>"[object object]"}
i tried using json.stringify
method in js, params hash became unreadable :
json.stringify(params)
gives
# parameters: {"0"=>"{", "1"=>"\"", "2"=>"l", "3"=>"i", "4"=>"s", "5"=>"t", "6"=>"\"", "7"=>":", "8"=>"{", "9"=>"\"", "10"=>"n", "11"=>"a", "12"=>"m", "13"=>"e", "14"=>"\"", "15"=>":", "16"=>"\"", "17"=>"m", "18"=>"a", "19"=>"m", "20"=>"a", "21"=>"\"", "22"=>",", "23"=>"\"", "24"=>"p", "25"=>"r", "26"=>"o", "27"=>"g", "28"=>"r", "29"=>"e", "30"=>"s", "31"=>"s", "32"=>"\"", "33"=>":", "34"=>"\"", "35"=>"0", "36"=>"/", "37"=>"0", "38"=>"\"", "39"=>",", "40"=>"\"", "41"=>"p", "42"=>"r", "43"=>"o", "44"=>"g", "45"=>"r", "46"=>"e", "47"=>"s", "48"=>"s", "49"=>"_", "50"=>"b", "51"=>"a", "52"=>"r", "53"=>"\"", "54"=>":", "55"=>"0", "56"=>",", "57"=>"\"", "58"=>"t", "59"=>"h", "60"=>"u", "61"=>"m", "62"=>"b", "63"=>"\"", "64"=>":", "65"=>"\"", "66"=>"d", "67"=>"e", "68"=>"f", "69"=>"a", "70"=>"u", "71"=>"l", "72"=>"t", "73"=>".", "74"=>"j", "75"=>"p", "76"=>"g", "77"=>"\"", "78"=>"}", "79"=>"}"}
update
i don't understand why, now, if pass list as-is (without trying nest inside params hash), , in rails, set attributes manually (@list.name = params[:name]
, so), problem solved.
i prefer write @list.update_attributes(params[:list])
though.
Comments
Post a Comment