0

I'm going to spend some of my easter vacation learning ruby on rails. I've encountered a problem with my routing. I hope you can help me with this.

What I'm trying to do is set my controller index in namespace home as the root (the controller I want to use when I hit the root of my website). Note that my controller is called index and the method I want to use is also called index.

Here is the structure of my controller(s):

app
  -controllers
     -home
        -index_controller.rb

My index_controller.rb looks like this:

class Home::IndexController < ApplicationController
  def index
     @testing = 1
  end
end

My routes.rb file looks like this:

MyFirstRail::Application.routes.draw do
    namespace :home do
        get "/" => "index#index"
    end
end

I had a look at this question - but I couldn't make it work.

I'm using rails 3 and Rubymine as IDE (if it's any help).

Community
  • 1
  • 1
Peter Rasmussen
  • 16,474
  • 7
  • 46
  • 63

2 Answers2

1

This is how I do it in my project:

root :to => "home::index#index"

The structure is always the same with root :to (which is what is used to define the root route :))

root :to => "controller_name#action"

Your namespaced controller here is simply named home::index.

siekfried
  • 2,964
  • 1
  • 21
  • 36
  • I am curious as to what the difference between using :: and / in the root is? Both seems to work. – Peter Rasmussen Mar 28 '13 at 13:08
  • I don't know if there is a big difference, I'm simply used to write the controller name instead of its path. You can choose the one you prefer. – siekfried Mar 28 '13 at 13:14
0

You can just try doing like this in routes,

root :to => "controller#action"
Srikanth Jeeva
  • 3,005
  • 4
  • 38
  • 58