How does a controller in ruby on rails get id by using params[:id] -


i working on creating basic blog using ruby on rails. new it. have 2 controllers named category , post_controller. both have show function shown:

for category controller:

category.find(params[:id]) 

for post_controller

post.find(params[:id]) 

when enter url

"localhost:3000/post_controller/2 

it displays posts id 2. when same thing category controller:

"localhost:3000/categories/2 

it shows error saying

no route matches [get] "/categories/1" rails.root: /home/root/myfirstblog 

and yes ofcourse, have element in categories database id 2. when use url:

"localhost:3000/categories/show?id=2 

it works difference between 2 controllers though have identical code.

p.s. confused information relevant please let me know in comments. edit question required.

rails.application.routes.draw    'categories/index'   'categories'=>'categories#index'   'categories/edit'   'categories/new'   'home/index'   'post_controller/edit'   'post_controller/index'   'categories/edit'   'categories/index'   'categories/new'   'categories/show'   resources :category   resources :post_controller   root 'post_controller#index' end 

first: resources :thing should pluralised

resources :things adds these routes.

so no need get 'categories/*' in routes file.

im not sure post_controller either. should renamed to. resources :posts

these things why seeing inconsistencies. without delving deep hard whats going on.

your routes file should like:

rails.application.routes.draw   resources :category # handles actions categories crud   resources :posts  # handles actions posts crud   root 'post_controller#index' #root view. i.e. "/" end 

Comments