Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does respond_to and respond_with work in rails?

When there is

def some_action
  respond_to do |format|
    format.html {}
    format.js {}
    format.json { respond_with @objects}
  end
end

It seems like html line and the js line automatically serve up/call the file matching the action's name. And the html and the js are serve up one or the other, not both. Is this correct?

The json gets called if you have an ajax call in your js that got called, and it requests data, and these need data to respond with, right? Do I need it to respond to json and to js, or just one?

If you don't to respond_to, and omit all the types, does it by default respond to html and to js?

When I do respond_to in the controller, rather than a respond_to block within each action, does using respond_with @objects apply to any argument (:thml, :js, :xml, :json, etc)?

Alternate syntax:

class TheController < ApplicationController

  respond_to :html, :js, :json, only: [:some_action, :other_action]

  def some_action
    respond_with @objects
  end
end

How does the alternate syntax work?

If you use the alternate syntax, can you not respond differently to different types of requests? Do you have to do a respond_to block isntead of the alternate syntax if you want to respond differently? How do each of these cases address graceful degradation to html?

like image 472
ahnbizcad Avatar asked Jul 14 '14 22:07

ahnbizcad


2 Answers

respond_with

For a given controller action, respond_with generates an appropriate response based on the mime-type requested by the client.

This basically means your controller will send the appropriate data on a request basis - for example, if you did the following:

#app/controllers/articles_controller.rb
Class ArticlesController < ApplicationController
   def show
       @article = Article.find params[:id]
       respond_with @article
   end
end

This would basically respond with the data from @article each time you send a request. If the request is in the json mime-type, it will come back as a JSON object; if it's an HTML request, it will come back with the HTML object on the show view

--

respond_to

Basically allows you to tailor the specific responses to different mime-types. If you send a JS request, you can manage the JS response etc

respond_to blocks inside the controller actions are very cumbersome, and only really meant for specific changes / edits to the response itself.

A much simpler way to handle the respond_to is to declare it at the top of the controller file, essentially telling Rails that every action will use the options defined in that method:

#app/controllers/your_controller.rb
Class YourController < ApplicationController
   respond_to :js, :json, :html #-> the same as using respond_to block for each action
end
like image 124
Richard Peck Avatar answered Oct 23 '22 18:10

Richard Peck


note that in Rails 4 respond_with feature has been extracted to the gem 'responders' (https://github.com/plataformatec/responders).

like image 7
lakesare Avatar answered Oct 23 '22 19:10

lakesare