Saturday 7 February 2015

what is mime type and respond_to in rails

respond_to:    and  MIME Type.
--------------------------------------------------------

respond_to is a method of rails that defines different formats that your actions, well , respond to.
And the formate of file is maintained in rails mime_type.

#require 'action_controller/mine_types'

#actionpack/lib/actioncontroller/mimetypes.rb 

 and if you need to use a MIME type which isn't supported by default, you can register your own handler in config/initializers/mime_types.rb

MIME::Type.register "image/jpg", :jpg

And Respond to also allows you to specify a common block for different formats by using any:

def index
  @people = Person.all

  respond_to do |format|
    format.html
    format.any(:xml, :json) { render request.format.to_sym => @people }
  end
end
here we see the block of respond_to is use for render different formats by using do block.

Now! one thing is important to notice the render file extension should be like this..
according to this block .. on index action it will be render two file ,
1st -- index.xml.erb, and 2nd is index.json.erb .






 

No comments:

Salesforce CRM vs. Zoho: A Comparative Analysis

Introduction: Selecting the right customer relationship management (CRM) software is crucial for businesses seeking to streamline their sal...