Why route root did not work? - ruby-on-rails

I am following the tutorial Ruby on Rails Tutorials by Michael Hartl and try to finish the last step in section 3.4.4. When I change routes.rb to
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
and relaunch http://localhost:3000/static_pages , they said "We're sorry, but something went wrong". Then I relaunch http://localhost:3000/static_pages/help and it works. Then I check the routes by
rake routes
And the result shows:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
I check the content in file static_pages_controller.rb and no difference as that in tutorial. Could someone tell me what is wrong?

Well, with this route mapping root 'static_pages#home', you said, when you hit http://localhost:3000/ url, you will be mapped to the home action of the StaticPages controller.
But you didn't define any mapping for http://localhost:3000/static_pages, it is incorrect url as per the route.rb file. That's why you got the error.
Read the first line of the output of rake routes, it is clearly telling what you have defined.

The line root 'static_pages#home' is equivalent to get '/', to: 'static_pages#home'. This means that the URL localhost:3000/ is being routed to the #home action on the StaticPages controller.
If you want localhost:3000/static_pages to point to the #home action, add this line:
get 'static_pages', to: 'static_pages#home'

Related

Routing Error No route matches [GET] "/index"

I'm new to Rails. I've got a routing error bellow when I try to access http://localhost:3000/index.
No route matches [GET] "/index"
Rails.root: /Users/[...]/taskleaf
Routes
Helper HTTP Verb Path Controller#Action
root_path GET / tasks#index
tasks_path GET /tasks(.:format) tasks#index
POST /tasks(.:format) tasks#create
[...]
Here is the content of my route.rb file:
Rails.application.routes.draw do
root to: 'tasks#index'
resources :tasks
end
I would be very grateful if you could help me out ...
from what you've provided, try changing it to either '/' or '/tasks/index' then you may get what you're looking for.
Or, add this code in your routes.rb file get '/index', to: 'tasks#index', which would direct /index path to the index action in your tasks controller.
Here is how Rails routing works: when you request '/index', it'll try to find #index route in your routes.rb file, which doesn't exist since the index action lies within your 'tasks' controller (from what I've seen from your codes). In the code, you defined the root page ('/') to be '/tasks/index' (try visiting '/' and '/tasks/index', they should be the same).
Remember, whenever you've seen a routing error, it is likely that you didn't define the route in routes.rb file
Feel free to give it a try and update here if there's any further errors/problems.
Cheers :)

How to get the sent parameters with POST (without form) on rails

I just only want to make a test and trying to understand the routes on rails. This is what I have on my controller:
class ItemsController < ApplicationController
def index
#data = params[:number]
end
end
and in index.html.erb
The number: <%= params[:number] %>
Well, if I made curl http://localhost:3000/?number=100 I can see on the view:
The number: 100
So, here everything is correct. But, I want to do the same, but with POST verb. So when I made curl -d "number=100" http://localhost:3000 I get the following error:
No route matches [POST] "/"
I have made:
def create
render plain: params[:number].inspect
end
to see the parameters, but as I said before, only works with GET verb.
So my question: How I can see the data sent by POST to the controller with curl and see the result on my view index.html.erb?
Note: On routes.rb I have:
Rails.application.routes.draw do
#get 'items/index'
resources :items
root 'items#index'
end
Notes: Based on the answer received, I have 2 more questions:
Why the verb GET works on http://localhost:3000/?number=100 and the same with http://localhost:3000/items/?number=100? Why the same does not happens with POST?
How can I remove the message No route matches [POST] "/" if the user points directly to http://localhost:3000 with POST verb?
You are posting to the root_url. Instead, POST your request to the items_url:
curl -d "number=100" http://localhost:3000/items
update:
Why the verb GET works on http://localhost:3000/?number=100 and the
same with http://localhost:3000/items/?number=100? Why the same does
not happens with POST?
The GET request to /?number=100 works because you have specified root 'items#index' in your routes file. This specifically creates a GET route that is mapped to the index action of the items controller.
How can I remove the message No route matches [POST] "/" if the user
points directly to http://localhost:3000 with POST verb?
You can create a single POST route using the post keyword:
# routes.rb
root 'items#index'
post '/', to: 'items#create'
which would generate the routes:
root GET / items#index
POST / items#create
(from your project directory run the command rails routes)
Or you can use the resource method to create all the CRUD paths:
resources :items, path: '/'
... which would create the following routes:
items GET / items#index
POST / items#create
new_item GET /new(.:format) items#new
edit_item GET /:id/edit(.:format) items#edit
item GET /:id(.:format) items#show
PATCH /:id(.:format) items#update
PUT /:id(.:format) items#update
DELETE /:id(.:format) items#destroy
Keep in mind that this may cause routing collisions if you try to add other resources to your app. If you need to add other resources, add them before these routes in the routes.rb file. Rails evaluates the routes file from top to bottom, so these resources would only load if no other paths match.
For more information see http://edgeguides.rubyonrails.org/routing.html

Server says no routes are defined even though they are

I have defined three routes in routes.rb
Rails.application.routes.draw do
root to: 'pages#lottery'
get 'pages/about'
get 'pages/contact'
get 'pages/lottery'
end
And when I run "rake routes" in my command line, I get the following:
Prefix Verb URI Pattern Controller#Action
root GET / pages#lottery
pages_about GET /pages/about(.:format) pages#about
pages_contact GET /pages/contact(.:format) pages#contact
pages_lottery GET /pages/lottery(.:format) pages#lottery
But when I got to the localhost:3000/pages/contact I get the error:
"No route matches [GET] "/pages/contact.html.erb"
And also "You don't have any routes defined!"
Does anybody know the problem?
Ugh rookie mistake. I had two tabs open in my command line. My rails s pages was in a different directory. Thanks for the help everyone, I appreciate it.
You should not be going to the URL /pages/contact.html.erb, it should be just /pages/contact.html.
Rails provides nice helpers to make it easy to get the right path, for example, pages_contact_path (from your rake routes).
Your pages_controller.rb should consist of three methods based on your routes listed.
pages_controller.rb
class PagesController < ApplicationController
def lottery
end
def about
end
def contact
end
end
views/pages/contact.html.erb
<p>Hello World</p>
Start up your rails server - rails s and navigate to localhost:3000/pages/contact and you should see Hello World

root route not working

In routes.rb file: root "pages#index"
pages_controller.rb file
class PagesController < ApplicationController
def index
render 'home'
end
def about
end
end
view named home.html.haml
rake routes returns
Prefix Verb URI Pattern Controller#Action
home GET /home(.:format) pages#home
about GET /about(.:format) pages#about
localhost just says "We're sorry, but something went wrong.", the console says ActionController::RoutingError (No route matches [GET] "/home"):
On Rails 4.2.0.beta2
*Edit for Rails version and console error
Indeed, rake routes says that a GET /home will match pages#home, the home action in pages controller.
Can you show us more of your routes.rb file ?
Moreover, why don't you rename your home.html.haml file to index.html.haml file, thus your index action can become
def index
end
If you don't specify which view to render, Rails will try to find a view with the name of your action.
I would update your routes.rb file to:
root 'pages#index'
get '/home', to: 'pages#index'
get '/about', to: 'pages#about'
As suggested by Benjamin, I do think that it would be a better design to rename your home.html.erb to index.html.erb. This way you can remove the render 'home' from your index method.
What is odd is that your rake routes output says that /home will be redirected to pages#home, which is a controller method that you don't have.

Rails redirect controller to root

I have a website with a controller named "Posts".
I would like to redirect "example.com/posts" to "example.com" as they display the same information.
I know this is done in the routes.rb file but after a few hours of searching I don't think I'll figure it out. Any help is appreciated, thanks!
I am using Rails 4.0 on Ruby 2.0
You can read all about redirection in routes, here: http://guides.rubyonrails.org/routing.html#redirection
get '/posts', to: redirect('/')
...
root to: 'posts#index'
In your routes.rb file make sure the root :to is at the top of your code. Then you would need to assign a redirect for the GET request you want to always sent there:
root :to => 'posts#index'
get '/posts', to: redirect('/')

Resources