1

The goal is to create a URL like this for a GET, REST API:

/manager/someID/report
example: /manager/2/report

I can get it to show in rake routes if do it this way:

get 'manager/:id/report', to: 'report#show'

But in some weblogs I read, thats the way unskilled developers write their routes! and looks like the better way is to use "nested resources" so I am banging my head over desk to get nested resources working the same way...but no success

this is what I have written so far:

  resources :manager, only: [:show] do
    resources :report, only: [:show], controller: 'report' do
     member do
       ## WAT ?!
     end
    end
  end

1 Answer 1

1

First, you might want to consider reading different blogs if they're calling routes like that "unskilled".

What you proposed is actually fine considering it's a non-standard RESTful route, and maybe even preferable in some cases. If you want an alternative approach, you have a couple different options. I don't think any one is more right than the other, but I prefer the first because it takes up less vertical space.

resources :manager, only: [:show] do
  get 'report' => 'report#show', on: :member
end

or

resources :manager, only: [:show] do
  member do
    get 'report' => 'report#show'
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

is this way of nesting, the ones you have and the even the second non-working one that I had, are these really meant for stuff that have a has_many, belongs_to relation? like Article and Comments... Because in my case the nature of "manager" and "report" is not really like that at all . So still would be viable to do it with nested? or my own one liner route was good too?
I'm not sure there's a right or wrong answer to those questions. I've been using Rails for a while and they all seem valid to me. The only change I would probably make would be to pluralize the report controller name so it follows Rails' conventions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.