I am working on a small assignment to create posts and divide them by categories. I have everything working but in my index.html I’m getting the following routing errors related to my links.
No route matches {:action=>"show", :controller=>"posts"}
No route matches {:action=>"edit", :controller=>"posts"}
undefined method `post_path' for #<#<Class:0x007fd3097f0ce0>:0x007fd3097c9370>
In posts/index.html.haml I have:
- @category.posts.each do |post|
%tr
%td= post.title
%td= post.description
%td= post.user_id
%td= post.category_id
%td= link_to 'Show', category_post_path //gives first error
%td= link_to 'Edit', edit_category_post_path //gives second error
%td= link_to 'Destroy', post,
:confirm => 'Are you sure?', :method => :delete //gives third error
In routes.rb I have:
resources :categories do
resources :posts
end
When I run rake routes I get:
categories_index GET /categories/index(.:format) categories#index
category_posts GET /categories/:category_id/posts(.:format) posts#index
POST /categories/:category_id/posts(.:format) posts#create
new_category_post GET /categories/:category_id/posts/new(.:format) posts#new
edit_category_postGET /categories/:category_id/posts/:id/edit(.:format) posts#edit
category_post GET /categories/:category_id/posts/:id(.:format) posts#show
PUT /categories/:category_id/posts/:id(.:format) posts#update
DELETE /categories/:category_id/posts/:id(.:format) posts#destroy
There is something wrong in my index that is making the application crash because I can access and see these with no problems:
/categories/:category_id/posts/:id (equivalent to show)
/categories/:category_id/posts/:id/edit (equivalent to edit)
Can someone please help me out? Thank you in advance.
The URL helpers need to know which category and post you’re interested in so you have to pass the specific category and post objects as parameters to the helpers. I think these should work: