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.
Related
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 :)
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
I'm struggling with this line in the _form: <%= simple_form_for(#post, url: blog_path) do |f| %>, which gives me the error:
No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id]
Keep in mind that in my routes I have: resources :blog, controller: 'posts', which is to say I am working off of a posts MVC, but I wanted /posts/ to be replaced by /blog/ in the routes.
posts_controller
def new
#post = Post.new
end
def edit
end
The _form works when I go to edit, but not create new.
routes
blog_index GET /blog(.:format) posts#index
POST /blog(.:format) posts#create
new_blog GET /blog/new(.:format) posts#new
edit_blog GET /blog/:id/edit(.:format) posts#edit
blog GET /blog/:id(.:format) posts#show
PATCH /blog/:id(.:format) posts#update
PUT /blog/:id(.:format) posts#update
DELETE /blog/:id(.:format) posts#destroy
model_path by default routing logic in Rails leads to blog#show => /blogs/:id
Change it to blogs_path.
Looking at you routes, I see obvious naming conflict, you must be defined routes wrong.
Be sure it looks like resources :posts, :as=>"blogs", both plural.
UPD
If you want to have only one blog, then resource :post, :as=>"blog", both singular.
But that means one actual input. I'm quite sure you speak of blog/post1, blog/post2, otherwise I can't see any sense in calling it blog?
sorry my english...
<%= simple_form_for(#post, url: blog_path) do |f| %> In this line you are redirecting the form to show (blog_path)
But , depending on the route , the show needs an id (blog GET /blog/:id(.:format)).
You must create a "create" method in the controller that receives the parameters strong ...
There is another method you can use: rails generate scaffold_controller posts
This will build the full CRUD , so you only have to configure the parameters
I hope that helps you
I got best of both worlds this way:
resources :blog, to: 'posts'
resources :posts
I know this is a newbie question, but I haven't seen an explanation and I'd like one.
What exactly does it mean when Rails issues a routing error like this:
Routing Error
No route matches {:action=>"show", :controller=>"library_imports", :library_id=>#<Library id: 1, ...
What puzzles me is that the message itself shows that my request is being routed to the show action of the library_imports controller. How does that happen if the request URL didn't match any routes?
For the sake of completeness, the URL I'm hitting that results in this error is:
http://localhost:3000/libraries/2/library_imports
which should map to the "index" action, not "show".
The relevant part of config/routes.rb is:
Import::Application.routes.draw do
resources :libraries do
resources :library_imports
end
And the pertinent portion of rake routes output is:
library_library_imports GET /libraries/:library_id/library_imports(.:format) {:action=>"index", :controller=>"library_imports"}
POST /libraries/:library_id/library_imports(.:format) {:action=>"create", :controller=>"library_imports"}
new_library_library_import GET /libraries/:library_id/library_imports/new(.:format) {:action=>"new", :controller=>"library_imports"}
edit_library_library_import GET /libraries/:library_id/library_imports/:id/edit(.:format) {:action=>"edit", :controller=>"library_imports"}
library_library_import GET /libraries/:library_id/library_imports/:id(.:format) {:action=>"show", :controller=>"library_imports"}
PUT /libraries/:library_id/library_imports/:id(.:format) {:action=>"update", :controller=>"library_imports"}
DELETE /libraries/:library_id/library_imports/:id(.:format) {:action=>"destroy", :controller=>"library_imports"}
The error message is misleading for what it doesn't say, rather than what it says. There is indeed no route that can be constructed from :controller => :library_imports, :action => :show, :library_id => #library, because that hash omits the required :id parameter.
It might be less confusing if there were some hint, like (did you leave out a required parameter?).
It's also useful to note that this error is generated (I believe) in the url_for helper, not in the dispatcher. So the request is not actually being routed at all.
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.