47

I am an experienced JAVA and C++ developer and I am trying to understand how rails works.

I got this code below:

respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url }
        format.js { render :json => @line_item, :mime_type => Mime::Type.lookup('application/json'), 
                :callback => 'javascriptFunction' }

and I've been searching the api that defines what I can pass inside the format.js {} but I could not find..

first of all: what kind of statement is format.js, is that a variable?

and most important: what attributes can I pass into format.js {} ? can you pass the direct link? I've searched over the http://api.rubyonrails.org/

2 Answers 2

118
respond_to do |format|
  format.js # actually means: if the client ask for js -> return file.js
end

js here specifies a mime-type that the controller method would send back as a response;
Default Rails mime-types.
If you try also with format.yaml:

respond_to do |format|
  format.js
  format.yaml
end

that will mean that your controller will return yml or js depending on what the client-side is asking;

{} in terms of ruby is a block; If you don't specify any rails will try to render a default file from app/views/[contoller name]/[controller method name].[html/js/...]

# app/controllers/some_controller.rb
def hello
  respond_to do |format|
    format.js
  end
end

will look for /app/views/some/hello.js.erb; // at least in Rails v. 2.3.

If you do specify block:

respond_to do |format|
    # that will mean to send a javascript code to client-side;
    format.js { render             
        # raw javascript to be executed on client-side
        "alert('Hello Rails');", 
        # send HTTP response code on header
        :status => 404, # page not found
        # load /app/views/your-controller/different_action.js.erb
        :action => "different_action",
        # send json file with @line_item variable as json
        :json => @line_item,
        :file => filename,
        :text => "OK",
        # the :location option to set the HTTP Location header
        :location => path_to_controller_method_url(argument)
      }

  end
5
  • What if you want to specify the js file to be called, instead of the convention of same file name as method name? I am trying to use the same js file for different controllers.
    – kakubei
    Jul 19, 2013 at 9:59
  • 2
    format.js { :render ... should be format.js { render .... I'd edit it, but I have to change more than 6 characters to save the changes.
    – d_rail
    Aug 15, 2013 at 4:15
  • stil, the question remains: what kind of statement is format.js, is that a variable?
    – Lokomotywa
    Aug 25, 2015 at 16:07
  • 2
    @Ferenjito, format is a variable from block respond_to do |format| , which holds an object. Calling .js on that object defines a behavior for javascript mimetype response. Sort of DSL
    – ted
    Oct 5, 2015 at 6:51
  • 1
    alone format.js throws me error > unknow format . but when i put format.html it allows me to go without entering in js.erb file . why
    – Mani
    Oct 14, 2015 at 8:31
16

I believe this was the url you were looking for:

https://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

This might also be helpful to some, to see that you can actually render js directly within the format.js method, if you for example only have a small one line js statement you want to return, and you don't want to defer to a RJS file like controller_action_name.js.erb:

respond_to do |format|
  format.html { redirect_to new_admin_session_path }
  format.js   { render :js => "window.location='#{ new_admin_session_path }'" }
end

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.