0

The problem is i want to have both nested resources and normal resources pointing the the same action in controller but act differently based on whether the nesting resource in available or not.

Routes.rb

 resources :users do
     resources :comments  //For having nested routes
 end
 resources :comments  //For having normal routes

Nested Resource

For example

Class CommentsController < ApplicationController

 def index
   @user = User.find(params[:id])
   @comment = @user.comments.find(params[:id])
 end

Now,

If i want to just look up all the comments , i want to use the

/comments

Else if i am in the user page and click on the all comments i get to

/user/:user_id/comments

Hence, how to configure to make sure it generates the right page.

Gunther Gib
  • 117
  • 10

1 Answers1

0

If I were you, I would do like this:

def index
  @comment = Comment.find_by(user_id: params[:user_id], id: params[:id])

  # It looked weird that you tried to find a @comment
  # instead of @comments in action `index`
  # I think you made a typing mistake and this can be help
  @comments = Comment.where(user_id: params[:user_id])
end
fongfan999
  • 2,565
  • 1
  • 12
  • 21