0

The data for my application is structured as:

  • Organizations have users
  • Organizations have messages
  • Organizations have sites

I would normally use nested resources to do this RESTfully:

resources :organizations do
  resources :users
  resources :messages
  resources :sites
end

Which gives me URLs like:

/organizations/12/messages
/organizations/12/sites

These wordy URLs are excellent for when an admin needs to create a message for an organization because the URL contains the organization ID.

However, I would like to show normal users pretty URLs such as:

/messages
/sites

Is there a way to alias these pretty URLs to /organizations/{current_user.organization.id}/* ?

4

1 回答 1

0

您需要一种传递组织 ID 的方法。例如,如果您有一个属于组织的用户并且该用户已登录,您可以通过 current_user 获取组织 ID,如下所示:

class MessageController < ApplicationController
 before_filter :get_organization
 # ...
private
  def get_organization
    @organization = current_user.organization
  end
end

而且,在您的路线中,您可以添加:

resources :messages
resources :organizations do
  resources :messages
end

还有其他解决方案,但这取决于您如何获取组织 ID。

于 2013-05-02T12:22:32.113 回答