0

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/followers?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.

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1
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...

1 Comment

A thousand thanks! I'm going to blog on this now so it's not so difficult to figure out. See - easy points!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.