Question

In an introduction to Ruby classes and inheritance in Ruby on Rails Rails Tutorial, Michael Hartl asks why, when running the static_pages_controller.rb in the rails console, an action returns nil when it is called on a StaticPagesController object:

class StaticPagesController < ApplicationController
  def home
  end
  ...
end

In the rails console:

>> controller = StaticPagesController.new
=> #<StaticPagesController:0x22855d0> (*)
>> controller.home
=> nil

(*)(my rails c returns attributes associated with the object, but they do not show up in the 2nd edition, but I'm using Rails 4.0.2, and he's on Rails 3, I believe, which might be the answer to my question, but we'll see)

Specifically, he asks the following:

But wait--actions don't return values, at least not ones that matter. The point of the home action ... is to render a web page, not to return a value. ... What's going on?

His answer is just that Rails is not Ruby:

Rails is written in Ruby, but Rails isn't Ruby. Some Rails classes are used like ordinary Ruby objects, but some are just grist for the Rails' magic mill. [emphasis added]

My question is, why does that count as the answer? When I run the same commands in IRB (after having created my own StaticPagesController class -- albeit it does not inherit from Rails' ActionController), I also get a nil return value:

My-Computer:app_root_directory David$ rails c
2.0.0p353 :001 > controller = StaticPagesController.new
2.0.0p353 :009 > controller.home
 => nil 
2.0.0p353 :010 > exit
My-Computer:app_root_directory David$ irb
cannot load such file -- hirb
>> class Controller
>> def home
>> end
>> end
=> nil
>> controller = Controller.new
=> #<Controller:0x007ff365299c70>
>> controller.home
=> nil

Basically, am I making a mountain out of a molehill by asking this question? Is it alright not understanding why he's making a point about why an action (which is really just a method of a controller class) returns nil when called in the Rails console?

Was it helpful?

Solution

I think you misinterpreted the question. If you look at a controller the way you look at an ordinary Ruby class, then the home method... makes no sense. It does nothing interesting (returning nil is kinda boring). As a Ruby programmer you wouldn't put it there, because Ruby programmer declares methods in order to use them (so they should return a value, or just do something observable)

In Rails on the other hand actions (even empty) are essential part of the MVC pattern. You declare those methods and never call them (at least not explicitly), because in Rails it's not just a method, it's a way of telling Rails what to do.

This is why he emphasizes that Rails is not Ruby, especially when you use it with gems like Capybara and Cocumber. They are Domain Specific Languages (DSLs). Yes, they use Ruby, but they are not Ruby.

OTHER TIPS

In ruby everything returns an object. So the statement "actions don't return values" isn't technically true. Without the context of the rest what's said I'm guessing he means calling an action in the Rails request lifecycle doesn't return a value you care about. Somewhere in the Rails stack the call to the action returns an object, but it is irrelevant to your development.

As for your console output, you are only getting nil because your action is empty. If you changed the action definition to:

def home
  1
end

Then you would receive 1 as your return object, both in the Rails context and outside of it. I think another key point is to consider nil a value, just one that represents the absence of a value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top