ruby-on-railsrubyroutes

How to make a single-layered path or route in ruby on rails


I'm new to Ruby on Rails, and I'm wondering if it is possible to create a single-layered path name in Rails.

As far as I know, you can only create routes like home/about. Is there a way to only create /about instead?

I tried doing this with get '/test' where test has its own controller and a single HTML page in views, but Rails threw an error called: ArgumentError: Missing :controller key on routes definition, please check your routes.

Does this mean I cannot create a path like /about, but I have to create a path like home/about? I don't think anybody else has asked this before.


Solution

  • You just have to tell Rails which controller you actually want to dispatch the request to:

    get :test, controller: :foos # implies that the action is test
    get :test, controller: :foos, action: :bar
    get :test, to: 'foos#bar' # shorthand for declaring controller and action
    

    Or for a group of routes:

    scope controller: :pages do
      get :about
      get :contact
    end
    

    These are commonly just referred to as non-resourceful routes and you can define them any way you want.