Question

I'm developing simple image sharing site to train my ruby-on-rails-fu. I have following resources in my config/routes.rb file.

resources :users do
  resources :images
end

resources :images

Here's the problem I'm facing - how should I go about implementing functionality like "latest images" and "your image subscriptions"? Having vanilla resource url's here's how it looks now:

/users/N/images # ImagesController#index action for listing all images for a user
/images # ImagesController#index action for listing all possible images from all users.

How would you go about managing the "parent" object of images when accessing index action in images controller? Would simple check for :user_id in params hash suffice? Are there any plugins I'm not aware of (since I could not think of a simple description for this problem).

Was it helpful?

Solution

I would assign one to another controller, so that you won't mix them up and confuse yourself.

resources :users do
  resources :images, :controller => user_images
end

resources :images

Now in user_images_controller, you may consider getting the user object by before_filter (cause it depends on a given user :D)

UserImagesController
  before_filter :get_user

  def get_user
    @user = User.find(params[:id])
    // You could also do error checking in before_filters
  end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top