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 :)
Related
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
So at the moment I am following a Tutorial for a Photoblog for Ruby on Rails (my version is 5.0.1)
And right now I have a constant routing error
My routes.rblooks as following, generated by Rails
Rails.application.routes.draw do
get 'posts/new'
get 'posts/create'
end
When I execute rake routes I get this
Prefix Verb URI Pattern Controller#Action
posts_new GET /posts/new(.:format) posts#new
posts_create GET /posts/create(.:format) posts#create
What makes me curious is, that when I access /posts/create manually, it is no problem at all like it should be.
But in /posts/new I fill out a Form which will be redirected and here is where the error occurs
<%= form_tag({:action => :create}, :multipart => true) %>
<fields to be filled in>
</form>
Anybody a clue?
My Routing Error looks like this:
No route matches [POST] "/posts/create"
No route matches [POST] "/posts/create"
Your route for create should be of type post
Change
get 'posts/create'
to
post 'posts/create'
A route is identified by an HTTP verb and a path. Your route is
get 'posts/create'
HTTP verb: GET
Path: posts/create
A form by default has a POST method (the HTTP verb), so you should change your route from
get 'posts/create'
to
post 'posts/create'
Yes, the above answers are right. The question can be closed. An alternative would be to add this to your routes.rb
resources :posts
this will give you all the common rails routes for a scaffold resource.
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'
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.
The following link works in my app:
<%= link_to "invitation", :controller => :invitations, :action => :index %>
To follow restful conventions i changed the link to:
<%= link_to "invitation", index_invitation_path %>
The error that i get is:
undefined local variable or method `index_invitation_path'
Rake routes yields:
invitations GET /invitations(.:format) {:controller=>"invitations", :action=>"index"}
The page name is index.html.erb. The model is invitation.rb. The controller is invitation_controller.rb. Routes has resources :invitations. What am i missing?
Thanks!
Assuming you have the routing correct:
resources :invitations
Then the correct helper for the index action (with the url /invitations.html) is
invitations_path
You can see more information by running rake routes. It will display text like the following:
lists GET /lists(.:format)
{:action=>"index", :controller=>"lists"}
POST /lists(.:format)
{:action=>"create", :controller=>"lists"}
new_list GET /lists/new(.:format)
{:action=>"new", :controller=>"lists"}
edit_list GET /lists/:id/edit(.:format)
{:action=>"edit", :controller=>"lists"}
list GET /lists/:id(.:format)
{:action=>"show", :controller=>"lists"}
PUT /lists/:id(.:format)
{:action=>"update", :controller=>"lists"}
DELETE /lists/:id(.:format)
{:action=>"destroy", :controller=>"lists"}
root /(.:format)
{:controller=>"lists", :action=>"index"}
The above was from a route of my own (for a model called List). The route helper method is shown immediately before the HTTP method. You have to remember to append the _path to each helper method. For example the helper methods I could use are:
list_path(list)
edit_list_path(list)
new_list_path
lists_path
You'll need a route in your routes.rb file that defines a mapping to the invitations controller and the index action.
Typically this is created with a resources call
resources :invitations
Which creates several default routes, which you can see by running rake routes.
For single resources, you can also define it using a match call
match "invitations/:id" => "invitations#index", :as => index_invitation
The rails site has a great resource on routing that provides all the details: Routing from the Outside In
Update: Based on your updated question, your route includes an invitaions (notice the trailing 's') route - nothing with index or invitation. The index_ prefix is generated by the resources call when it creates the default routes for :invitations.
It looks like you've defined a custom get mapping for an invitation. While this may technically work, if you're aim is to support restful routes, use the resources method. And have a read of the Routing guide from rails it's very easy to follow and quite detailed.
type rake routes in your console and look at listing of available routes. Seems to be there is no such route index_invitation_path? maybe it named differently
I think you need "invitations_controller.rb" to contain InvitationsController. Plural.