Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New To Rails 3, ajax request with json response

I have a controller named CourseRequests which will be accepting an ajax request for the "new" method.

  1. Since it will be responding with json, should I use /course_requests/new.json?

  2. I don't want to make a template for such a silly json response, how would I do that?

  3. What does respond_to do? (I have seen it block style and I understand that, but what about non-block style)

like image 871
Chris Muench Avatar asked Mar 07 '11 18:03

Chris Muench


1 Answers

  1. Yes, your JS would be doing something along the lines of:

    $.getJSON("/course_requests/new.json",...);
    
  2. You don't have to, you just need to have a respond_to block with JSON handled there.

    respond_to do |format|
      format.json { render :json => "test" }
    end
    
  3. Blockless syntax, I think you mean this: http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/ . Basically, you specify what mime types your controller responds to and then you can use the cooler responds_with method.

like image 72
Tomas Avatar answered Sep 30 '22 02:09

Tomas