Home > OS >  clean way to read out id in a request path in ruby on rails
clean way to read out id in a request path in ruby on rails

Time:04-30

I have a request that comes in like this: api/v1/photos/1/media/2

"1" and "2" are ids I need to read out in the controller. "2" I can get through the parameters: params["id"].

What's a clean way to read out the first id "1"? I can get the entire path with request.path or request.url. But since this returns a string I would have to do some string manipulation to get to the id. Is there a better/cleaner way to do this?

The routes are defined so:

resources :photos do
  member do
    resources :media
      member do
      end 
  end
end

i tried printing out params but it only had the second id and the action.

CodePudding user response:

Have you tried what params return if routes are like this?

resources :photos do
   resources :media
end
  • Related