Setting root of my website to a controller in a namespace - ruby-on-rails

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).

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.

You can just try doing like this in routes,
root :to => "controller#action"

Related

Getting 'Unknown action in controller'

I'm getting this error :
"The action 'create' could not be found for ObjectController"
I know it should be obvious but I'm missing something, that's my controller :
class ObjectController < ApplicationController
def index
end
def create
end
end
And that is my routes :
Rails.application.routes.draw do
get 'object/index'
get 'object/create'
match ':controller(/:action(/:id))', :via => :get
resources :objets
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'object#index'
You probably want to scrap those routes and try something simpler like
resources :objects, only: [:get, :create, :show]
Then use
$ rake routes
To make sure your routes are as the should be. You will want a POST route to /objects to create a new object etc..
Ok that one was dumb, actually I had two directories and I wasn't modifying the right one, sorry about that...
Your routes could be greatly improved:
#config/routes.rb
Rails.application.routes.draw do
root 'objects#index'
resources :objects
--
Next, the "standard" way to achieve what you're looking for is to use the new action; IE not the "create" action. If you wanted to use the create path name (instead of new), you'll be able to define it in the path_names argument:
#config/routes
resources :objects, path_names: { new: "create", create: "create" } #-> url.com/objects/create
To understand why you should be using new instead of create, you should look up resourceful routing, and how it pertains to object orientated programming.
Finally, your controller should be named in the plural:
#app/controllers/objects_controller.rb
class ObjectsController < ApplicationController
...
end
Whilst you can call it whatever you like, Rails defaults to plural controller names, singular model names.

Separate Domain for Namespaced Routes in Rails 4

I'm working on a Rails 4 app. One part of the app is a customer portal that has to be accessed from a separate domain.
I have everything working fine by navigating to domain.com/cp. The routes use namespaced controllers:
namespace :cp do
get :dashboard, to: 'dashboard#index', path: ''
...
end
How should I set up DNS records and change the routes definition so that another domain cpdomain.com points to domain.com/cp properly (no redirecting).
Thanks.
This answer can be useful for the rails routes problem:
Rails routing to handle multiple domains on single application
Shortened:
1) define a custom constraint class in lib/domain_constraint.rb:
class DomainConstraint
def initialize(domain)
#domains = [domain].flatten
end
def matches?(request)
#domains.include? request.domain
end
end
2) use the class in your routes with the new block syntax
constraints DomainConstraint.new('mydomain.com') do
root :to => 'mydomain#index'
end
root :to => 'main#index'
or the old-fashioned option syntax
root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')

Rails namespace root error

I'm running into a couple of errors when i try to define a root to a namespace. To recreate this, I'll rebuild a project from scratch.
rails new rails_test
cd rails_test
rails generate controller admin
rake db:migrate
Now I put some boilerplate in app/controllers/admin_controller.rb
class AdminController < ApplicationController
def index
end
end
in app/views/admin/index.html.erb
<p>Index</p>
and finally in config/routes.rb
root to: 'admin#index'
This all works perfectly, when i start the server and hit '/' (root) url (it goes to the admin index page), but it's not what I want.
This is meant to be part of a bigger project and I want to hit the /admin url and get the admin index so (following from http://guides.rubyonrails.org/routing.html#using-root) I change routes.rb to:
namespace :admin do
root to: "admin#index"
end
but I get a routing error:
uninitialized constant Admin
with a routes list containing 1 line:
admin_root_path GET /admin(.:format) admin/admin#index
My thinking from reading the end of this last line is that I'm already in the admin namespace so maybe i don't need to specify the controller index is in so I try changing routes to:
namespace :admin do
root to: "index"
end
But that gives me an ArgumentError saying "missing :action" on the 'root to: "index"' line.
I can get around it by using scope, but it looks like using namespace is a bit cleaner and I want to understand whats going wrong here.
Ruby/Rails versions
ruby -v -> ruby 1.9.3p392 (2013-02-22) [i386-mingw32]
rails -v -> Rails 4.0.0
Any help is apprechiated
namespace namespaces controllers as well. Change yours to
class Admin::AdminController < ApplicationController
def index
end
end
And move it under app/controllers/admin. That's what admin/admin#index means. :)
According to rubyonrails.org guide on routing you should be able to do something like this.
it should looks like this
namespace :admin do
root to: "admin#index"
end
root to: "home#index"
What do you get with rake routes?
on rails 4 in routes two roots not permited now
use
get "/admin" => "admin/admin#index", :as => "admin"
Type in terminal
rails generate scaffold Admin::User username email
rake db:migrate
if only Controller type this
rails generate controller Admin::User
I think what you really want is something like
namespace :admin do
get 'other'
end
get 'admin' => 'admin#index'
This allows for the index and other methods to be in straightforward AdminController controller in usual directory, views to go in views/admin etc.
You could probably also use
resource :admin, only: [:index] do
get 'other'
end

Namespace, static pages, and inherited controllers, what files in what folders?

I'm building an admin control panel (attempting to ;) ).
I have been looking at Backend administration in Ruby on Rails and as suggested I am trying to make Admin::AdminController that checks for admin and sets the layout etc.
But I'm also trying to set a route in it that routes /admin to /admin/dash
From my understanding of reading http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing , specifically section 2.6,
Admin::AdminController
tells rails that Admin is the name space, AdminController is the controller which is a subclass (extension?, implementation of the interface?) of ApplicationController. Which would imply the controller should live in app/controllers/admin/ and be called admin_controller.rb.
But what I want is
AdminController
I get errors like:
uninitialized constant Admin::Controller
My code for the route:
match :admin, :to => 'admin/admin#dash'
namespace :admin do
# Directs to /admin/resources/*
match '/dash', to: '#dash'
resources :users, :pictures
end
I have put the controller in app/controllers/admin, app/controllers and the combinations with
class Admin::AdminController < ApplicationController
before_filter :admin_user
# / ** STATIC ADMIN PAGES ** /
def dash
end
end
or class AdminController < ApplicationController.
Edit: Maybe it's my understanding of routing.
Example:
namespace :admin do
get "/dash"
vs.
namespace :admin do
match "/dash" to "admin#dash"
vs.
namespace...
match "/dash" to "#dash"
The first one makes it so i can display a dash via the controller, i.e. admin/dash would be controlled by
AdminController < ApplicationControler
def dash
end
Does the second one route admin/admin/dash to admin/dash?
TL/DR:
I think my confusion comes from syntax or my poor understanding of RESTful practices or maybe even class / object inheritance in ruby.
Thanks for helping this n00b out. :)
Side question: can I change my code to be minimized until someone opens it like a spoiler so it doesn't crowd things up if I find more information and add it?
I think your initial approach was correct, but you need to change it a little.
1) insert the /admin => /admin/dash inside the namespace (imho, its better to redirect it)
match 'admin' => redirect('admin/dash')
or
namespace :admin do
match '/', to: 'admin#dash'
end
2) You can't match '/dash' to '#dash' since you're not inside a resource block, you're inside a namespace block, so it doesnt' have implied controller.
namespace :admin do
match 'dash', to: 'admin#dash'
# This will go to Admin::AdminController#dash
# (first Admin because of the namespace,
# and the second because of the controller name)
end
hope it works :D
What you want is "scope" in routing.
scope "/admin" do
resources :articles
end
Which will route /admin/articles to ArticlesController (without Admin:: prefix)
Documentation covers almost every possible case.
http://edgeguides.rubyonrails.org/routing.html

Routing error in Ruby on Rails 3

I am new to Ruby on Rails
I am getting this error
uninitialized constant WelcomeController
after creating the sample project. I enabled
root :to => 'welcome#index'
in routes.rb.
When you say
root :to => 'welcome#index'
you're telling Rails to send all requests for / to the index method in WelcomeController. The error message is telling you that you didn't create your WelcomeController class. You should have something like this:
class WelcomeController < ApplicationController
def index
# whatever your controller needs to do...
end
end
in app/controllers/welcome_controller.rb.
I'm very very new to Rails and also ran into this error while following along with Rails Tutorial by Michael Hartl. The problem I had was that in the config/routes.rb file, I just uncommented the root :to => "welcome#index":
# just remember to delete public/index.html.
root :to => "welcome#index"
but with the structure of the sample_app was that "welcome#index" should be 'pages#home' instead, since everything was originally set up through the "pages" controller.
root :to => 'pages#home'
It's even right there in the book, but I just overlooked it and spent quite a while afterwards trying to figure out where I went wrong.
Make sure WelcomeController is defined in a file called welcome_controller.rb
rails generate controller welcome index
If you not generate the page with name welcome, then just generate the page like: $ rails generate controller pagename index. So then into the: config->routes.rb you should edit root 'welcome#index' to root 'pagename#index'
Keep this if you want it to be your context root after you generate your welcome parts.
Rails.application.routes.draw do
root 'welcome#index'
end

Resources