0

我正在开发简单的图像共享站点来训练我的 ruby​​-on-rails-fu。我的config/routes.rb文件中有以下资源。

resources :users do
  resources :images
end

resources :images

这是我面临的问题 - 我应该如何实现“最新图像”和“您的图像订阅”等功能?拥有香草资源网址是现在的样子:

/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.

在图像控制器中访问索引操作时,您将如何管理图像的“父”对象?简单:user_idparams哈希检查就足够了吗?有没有我不知道的插件(因为我想不出这个问题的简单描述)。

4

1 回答 1

1

我会将一个分配给另一个控制器,这样您就不会混淆它们并混淆自己。

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

resources :images

现在user_images_controller,您可以考虑通过 before_filter 获取用户对象(因为它取决于给定的用户:D)

UserImagesController
  before_filter :get_user

  def get_user
    @user = User.find(params[:id])
    // You could also do error checking in before_filters
  end
于 2010-10-17T07:46:20.983 回答