开发者

Problem with same resource, accessible via two routes

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 prob开发者_StackOverflow中文版lem).


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜