开发者

jQuery Ajax Call To Ruby Controller

开发者 https://www.devze.com 2023-03-03 12:19 出处:网络
I have a simple ruby on rails project that I\'m trying to use a simple jquery post with and, well I\'m stuck.I think I\'m expected this to work like ASP.NET MVC and it aint.

I have a simple ruby on rails project that I'm trying to use a simple jquery post with and, well I'm stuck. I think I'm expected this to work like ASP.NET MVC and it aint.

Here is what I have in the controller

def followers(username)
  return username
end

And in my .erb file I have the call to the method

 $.post("home/followers", { username: $("#txtUsername").val() },
   function(data) {
      alert(data);
   });

And what I get back is an error telling me: wrong number of arguments (0 for 1)

So I tried appending the parameter to the query string like...

 $.post("home/followe开发者_开发问答rs?username=burkeholland",
      function(data) {
          alert(data);
       });

This should be easy points for somebody. And would you believe I couldn't find anything about this via Google? There is a dirth of Ruby on Rails examples available online. Or maybe I'm just spoiled from ASP.NET.


def followers
 render :text => params[:username]
end

All post/get parameters are automatically given to you in the params hash. You should not declare any arguments to the method.

EDIT: Forgot to render the response instead of returning...


Methods in controllers cannot except parameters. The parameters are available via method called 'params'. After you process the request, you have to use 'render' method to send a response back to the browser. Such as:

def followers
  render :text => params[:username]
end
0

精彩评论

暂无评论...
验证码 换一张
取 消