Marwan Tanager
Marwan Tanager

Reputation: 369

Where is Ruby documentation for Mime::HTML

Let's say we have the following auto-generated code in a controller:

def update
  respond_to do |format|
    if @person.update(person_params)
      format.html { redirect_to @person, notice: 'Person was successfully updated.' }
      format.json { render :show, status: :ok, location: @person }
    else
      format.html { render :edit }
      format.json { render json: @person.errors, status: :unprocessable_entity }
    end
  end
end

And lets say that I'm curious enough to want to know what object format returns in order to know what object is json or html invoked on. Where is the documentation to answer those simple questions?

I tried ri format, and got this (among others):

Returns the MIME type for the format used in the request.

GET /posts/5.xml   | request.format => Mime::XML
GET /posts/5.xhtml | request.format => Mime::HTML
GET /posts/5       | request.format => Mime::HTML or MIME::JS, or request.accepts.first

If I do ri Mime::HTML, I get this:

Nothing known about Mime::HTML

But there is documentation for Mime::Type (which I think is what format above returns), and it states that this class has an instance method, method_missing, which I suppose is what invoked when we do format.html or format.json. What I want to know is: If I'm right about the method_missing fallback trick, then why there is no documentation for Mime::HTML.method_missing for example? And why there is no documentation for Mime::HTML or Mime::XML at the first place? In other words, what is the relation between Mime::HTML or Mime::XML, and Mime:Type?

Upvotes: 0

Views: 79

Answers (1)

Jun Zhou
Jun Zhou

Reputation: 3070

Read this:

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/mime_types.rb

And this:

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/mime_type.rb#L162-L174

To answer your question: Mime::HTML and Mime::XML are dynamically-generated constants, using Mime::Type.register.

Upvotes: 1

Related Questions