Problem: passing post parameters to a controller

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+"&notes="+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?

On Fri, Jun 11, 2010 at 5:37 PM, Max Y. [email protected] wrote:

Hi, I have a problem passing parameters via post.

post_params=“name=”+name+"&notes="+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

That did it!

Thank you very much Rick!!