ruby on rails - How to call controller from ajax -


i have graph (based on d3) in rails app. users click on graph , i'm hoping click result in showing page in rails app based on name passed show action or users controller. problem keep on getting response:

activerecord::recordnotfound in userscontroller#show

which because inadverntently passing parameters

{"id"=>"show"} 

which dont know how correct.

the ajax call (with bit of javascript d3.js name from) is:

    var node = svg.selectall(".node")       .data(graph.nodes)       .enter().append("circle")       .attr("class", "node")       .attr("r", function(d) { return d.group * 3; })       .style("fill", function(d) { return color(d.group); })       .call(force.drag)     //   .on('click', connectednodes)       .on("click",  function(d) { getprofile(d.name); });  //       function getprofile(name){ //     console.log(name); // };          function getprofile(d){       $.ajax({         url: "/users/show" ,         type: "get",         datatype: 'json',         data: { name: json.stringify(d.name) }     })}; 

and controller is

 class userscontroller < applicationcontroller  ... def show     @user = user.find(params[:name]) end end 

i guess want parameters

{"name" => "the_name_of_the_d.name_i_have_passed} 

my routes follows

prefix verb   uri pattern               controller#action graph_index    /graph/index(.:format)    graph#index  graph_data    /graph/data(.:format)     graph#data                /graph/index(.:format)    graph#index       users    /users(.:format)          users#index             post   /users(.:format)          users#create    new_user    /users/new(.:format)      users#new   edit_user    /users/:id/edit(.:format) users#edit        user    /users/:id(.:format)      users#show             patch  /users/:id(.:format)      users#update             put    /users/:id(.:format)      users#update             delete /users/:id(.:format)      users#destroy 

how can this?

edited:

when calling getprofile() function passing d.name, should change into: getprofile(d)


your controller calling params[:name], in ajax have change data: { name: json.stringify(d.name) }


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -