Ruby on Rails not recognizing route - ruby-on-rails

I'm trying to learn Ruby on Rails, from here: http://guides.rubyonrails.org/getting_started.html , But when I try to set the root for the route up in Section 4.3, although the route is recognized, when I run the server and localhost:3000, it still thinks there's no root route. What could the problem be? I'm using Ruby 1.9.3.

Routes
Without your routes file, or even the error which is being presented, I'll just give you what you need to get the "root" route working:
#config/routes.rb
root "application#index" #-> syntax - "controller#action"
This has to be backed up with the appropriate controller action:
#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
def index
end
end
It also needs the appropriate views:
#app/views/application/index.html.erb
This is the root page!

Related

undefined method `get' for controller

I'm trying to use a bit of code from a Ruby application in Rails.
The application in question is not in Rails and has two views. I copied a part of the code and pasted it in a controller in Rails:
get '/' do
erb :index, :locals => {:item_id => item_id, :access_token => access_token}
end
And I'm getting the error:
ActionController::RoutingError (undefined method `get' for XXXcontroller)
I'm not sure how I'm supposed to interpret this bit of code in a controller in Rails.
You need to start with an actual tutorial/book and learn Rails. You can't assemble a rails app out of random snippets that you have no understanding of.
That code is from Sinatra which is designed with simple applications in mind and where your routes and controllers are mushed together into a single file. Rails and Sinatra code is not interchangeable*.
If you want to define a route in Rails for / (the root path). You define it like so:
# config/routes.rb
Rails.application.routes.draw do
root to: 'pages#home'
end
And then declare the corresponding controller:
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
def home
end
end
And a view:
<h1>Pages#home</h1>
<p>Find me in app/views/pages/home.html.erb</p>
See:
Getting Started with Rails
Routes in a Rails app are located in the routes.rb file in the config directory. Your route might look something like this:
get '/index', to: 'controller#index'
This says execute the index action of the controller controller. That method (index) is associated with a ‘get’ request.

Rails keeps displaying home page on root even when I delete the method in the Pages Controller

This is more of a question than a real issue, but I created a new rails project. I've changed the
Rails.application.routes.draw do
get 'pages/about'
get 'pages/contact'
devise_for :users
root to: 'pages#home'
end
and my Pages controller looks like this
class PagesController < ApplicationController
skip_before_action :authenticate_user!, only: [:home]
def home
end
def about
end
def contact
end
end
My homepage gets displayed even if I remove the home method.
I just wanted to know more about how this works.
I've looked around before posting and couldn't find an answer.
This is my first post here.
Thanks in advance
This is part of Rails 'convention over configuration' mantra. See Rendering by Default: Convention Over Configuration in Action
From the guide:
By default, controllers in Rails automatically render views with names that correspond to valid routes.
So, even if the home action is undefined, Rails will still render the home view because there is a valid route.

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

Rails namespaced routes to wrong Controller

So I'm just beginning with RoR and figured I do a basic blog with API endpoints aswell. The problem is that my api requests seem to be routed to the wrong controller,
I have the following as my routes.rb
Blog::Application.routes.draw do
namespace :api do
namespace :v1 do
resources :articles
end
end
end
I also have controllers/api/v1/articles_controller.rb, which has the following content:
module API
module V1
class ArticlesController < ApplicationController
respond_to :json
def index
respond_with Article.all
end
end
end
end
My logic says that when I hit http://localhost:3000/api/v1/articles, this should be the Controller to respond, however the actual Controller that responds is the one in the root of controllers (controllers/articles_controller.rb) and not the one in the /api/v1 path. When I remove the Controller that actually responds, I'll get uninitialized constant Api::V1::ArticlesController instead.
Even rake routes gives me the expected routes, however actually hitting those endpoints fails. Output of rake routes is the following:
api_v1_articles GET /api/v1/articles(.:format) api/v1/articles#index
POST /api/v1/articles(.:format) api/v1/articles#create
new_api_v1_article GET /api/v1/articles/new(.:format) api/v1/articles#new
edit_api_v1_article GET /api/v1/articles/:id/edit(.:format) api/v1/articles#edit
api_v1_article GET /api/v1/articles/:id(.:format) api/v1/articles#show
PUT /api/v1/articles/:id(.:format) api/v1/articles#update
DELETE /api/v1/articles/:id(.:format) api/v1/articles#destroy
The only similar question I found on SO is nested namespace route going to wrong controller however, there's no accepted answer there and it's been a year. Maybe another attempt will help resolve this issue
Your module is API, but Rails is looking for Api. Ruby's modules are case-sensitive.

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