Reputation: 697
I am working on creating a basic blog using ruby on rails. I am new to it. I have two controllers named category and post_controller. Both have a show function as shown:
for category controller:
Category.find(params[:id])
for post_controller
Post.find(params[:id])
When I enter the url
"localhost:3000/post_controller/2
It displays the posts with id 2. But when I do the same thing for the category controller:
"localhost:3000/categories/2
It shows an error saying
No route matches [GET] "/categories/1"
Rails.root: /home/root/myFirstBlog
And yes ofcourse, I have an element in my categories database with id 2. However when I use this url:
"localhost:3000/categories/show?id=2
It works obviously but what is the difference between the two controllers even though they have identical code.
P.S. I am confused as to what information will be relevant so please let me know in the comments. I will edit my question as required.
Rails.application.routes.draw do
get 'categories/index'
get 'categories'=>'categories#index'
get 'categories/edit'
get 'categories/new'
get 'home/index'
get 'post_controller/edit'
get 'post_controller/index'
get 'categories/edit'
get 'categories/index'
get 'categories/new'
get 'categories/show'
resources :category
resources :post_controller
root 'post_controller#index'
end
Upvotes: 1
Views: 1380
Reputation: 4956
First: The resources :thing
should be pluralised
resources :things
adds these routes.
So no need for all the get 'categories/*'
in your routes file.
Im not sure what post_controller
is either. Should probably be renamed to. resources :posts
These things are probably why you are seeing inconsistencies. Without delving into it too deep its hard to say exactly whats going on.
Your routes file should look something like:
Rails.application.routes.draw do
resources :category # handles actions for all categories CRUD
resources :posts # handles actions for all posts CRUD
root 'post_controller#index' #root view. i.e. "/"
end
Upvotes: 1
Reputation: 101
It seems you misspelled the the controller in the URL. If your controller is categories localhost:3000/categories/2 should work.
Upvotes: 0