Hi, I have a problem passing parameters via post. I''ve examined the log file of a scaffolded version against my AJAX version and can''t figure out what''s wrong. I think I''ve also isolated the problem to be parameter passing by using logger.debug. the AJAX part; var name=document.getElementById("name").value; var notes=document.getElementById("notes").value; var parent_nid=document.getElementById("parent_nid").value; var post_params="name="+name+"¬es="+notes+"&parent_nid="+parent_nid+"&authenticity_token="+window._token; var req=XMLHttpRequest(); req.open("POST","/categories/create",true); req.setRequestHeader("Content-type","application/x-www-form-urlencoded"); req.send(post_params); The controller part: (mostly from scaffold) @category=Category.new(params[:category]) logger.debug "name:" logger.debug @category.name logger.debug "notes:" logger.debug @category.notes logger.debug "parent_nid:" logger.debug @category.parent_nid Category.create(@category) redirect_to :controller=>''dashboard'' the log part: Parameters: {"name"=>"abc", "authenticity_token"=>"d8c79e2216df723f373a9170fa364f9a3c147092", "notes"=>"abcde", "parent_nid"=>"0"} name: notes: parent_nid: -- Anyone has any idea what''s wrong? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Jun 11, 2010 at 5:37 PM, Max You <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, I have a problem passing parameters via post.> post_params="name="+name+"¬es="+notes+"&parent_nid="+parent_nid+"&authenticity_token="+window._token;...> the log part: > Parameters: {"name"=>"abc", > "authenticity_token"=>"d8c79e2216df723f373a9170fa364f9a3c147092", > "notes"=>"abcde", "parent_nid"=>"0"}The parameters aren''t nested properly, you want. { "category" => {"name"=>"abc", "notes" => "abcde", "parent_nid" => "0"}, "authenticity_token"=>"d8c79e2216df723f373a9170fa364f9a3c147092" } I also suspect that the parent_nid attribute of category probably should be parent_id (based on convention assuming that a category belongs_to :parent) So I think you want something like this javascript: post_params="category[name]="+name+"&category[notes]="+notes+"&category[parent_id]="+parent_nid+"&authenticity_token="+window._token; HTH -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
That did it! Thank you very much Rick!! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.