Search code examples
rubyruby-on-rails-3.1webrick

How to get Processing Type from Controller (Rails)


When an HTTP request comes into the server (in this case I'm running rails s which defaults to webrick) the message looks something like:

Started POST "/cards" for 127.0.0.1 at 2011-11-05 15:04:29 -0400
  Processing by CardsController#create as JS

I'm wondering how to get the value JS in this case, or HTML. Sorry I can't be more explicit, I'm not sure what that value is called. Anyone have any ideas?

Thanks


Solution

  • You can use request.format to get the format. It'll return "text/javascript" for JS requests, "text/html" for HTML, "application/json" for JSON, etc.

    Also, you would typically use this in a respond_to block:

    respond_to do |format|
      format.html { do something }
      format.js { do something else }
    end