← Back to Upcase

Rails controller (confused)


(Arif Hossain) #1

Hello,
I am bit confused with Rails controller!
In my routes file i have:

get “about” => "pages#about"
In my controller i have:

def action
  
end

Then i got an error but if i create an about page in views, it works!!!
Can anyone explain why it’s working, please?

thanks


(Luís Ferreira) #2

The methods in controllers are called actions, but they can have any name. In your case, you specify in the routes that the /about route will map to the PagesController and the about action. Therefore your controller should look like this:

class PagesController < ApplicationController
  def about
  end
end

Since having an empty action in the controller does not add much, Rails is intelligent enough to render the about page from the views if you have one. I would not advise you to take advantage of this to often as it may lead to some confusion. Prefer to explicitly define all the actions in the controller.

Hope it helps.


(Arif Hossain) #3

Got it.
Thanks


(Scott Hollinshead) #4

You could also do this using the High Voltage Gem, this gem makes creating static pages very easy.