Just curious if anybody knows what Ruby technique is used to accomplish the following in the Rails framework.
If I don't write, say, an index
method on a Rails controller, Rails will still render the index view file if the URL matches that route. That makes sense, because my controller inherits from a parent class, which must have its own index
method.
However, if I do define an index
method, and only tell it to set an instance variable, it still renders the appropriate view. For example:
def index
@weasels = Weasel.all
# If I omit this line, Rails renders the index anyway.
# If this behavior is defined in the parent class's index method,
# it seems that by overriding the method, my index wouldn't do it.
# I'm not explicitly calling super to get the method from
# ActionController::Base, but maybe Rails is doing something like that?
render :index
end
In pure Ruby, I would expect to have to call super
to get that behavior.
I assume that Rails uses some kind of metaprogramming technique to guarantee that my controller methods will call super
. If so, can anyone explain it? Can you point to the source code that does this?
As Mladen Jablanović has pointed out, my initial mental model was wrong; the controller method doesn't normally render the view; instead, both the controller method and the view rendering are called by some framework code. This is apparent because I can make a controller method with any name - for example, search
- and the search
view will get rendered. So clearly I'm not overriding a parent method in that case, and some framework code is parsing the controller method name and looking for the matching view.
Still, the framework must be able to detect whether or not the controller method has already called render
. So that's another small mystery to me.
Rails can render a raw file from an absolute path. This is useful for conditionally rendering static files like error pages. This renders the raw file (it doesn't support ERB or other handlers). By default it is rendered within the current layout.
Rendering is the ultimate goal of your Ruby on Rails application. You render a view, usually . html. erb files, which contain a mix of HMTL & Ruby code. A view is what the user sees.
A Rails View is an ERb program that shares data with controllers through mutually accessible variables. If you look in the app/views directory of the library application, you will see one subdirectory for each of the controllers, we have created: book.
Routing decides which controller receives which requests. Often, there is more than one route to each controller, and different routes can be served by different actions.
On the controller, there is a method called render_for_text
. This takes a string and sets the result as the response body. Even if you don't render as text, rendering a view file simply reads the file's contents, evaluates it, and passes it to the render_for_text
method. The method then stores sets an instance variable called @performed_render
to true
, which tells rails that the controller has already rendered a view.
There is then a method called performed?
that indicates whether or not the render action has been called. It does this by checking if @performed_render
or @performed_redirect
is true.
With the above information, the very first line of the render method should make sense now, and hopefully answer your question:
raise DoubleRenderError, "Can only render or redirect once per action" if performed?
When rendering a view, Rails initializes an instance of the appropriate view template with copies of the instance variables in the controller. So the instance variables aren't actually inherited by the view, but rather copied from the controller into the view before the view is rendered.
I haven't looked up the exact details in the source, but the above explanation should at least outline the general idea of how it works.
Jamis Buck wrote quite a bit about implicit routes a few years ago here.
I believe the code you're looking for is in Resources. Rails looks at the inbound request and (for RESTful routes) goes here to determine the controller and action if they aren't specified.
Update: Controllers are a really complex part of the Rails framework. It isn't quite as simple as a single Ruby class with a single method (which is why super
isn't really needed, but a series of calls both before and after your individual method is called.
Rack handles the request and passes it to Rails which does the routing, delegates to the instance of ActionController for the action (which may or may not have been coded by you), then passes the result of that to the rendering process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With