我的rails应用程序是基于帐户的。因此,每个用户都属于一个帐户,每个项目等等。
目前,我有这样的路由:
/mission-control
/tasks
/projects我正在通过用户获取当前帐户。由于用户应该能够拥有多个帐户的权限,因此我希望具有以下路由:
/:account_id/mission-control
/:account_id/tasks
/:account_id/projects我知道我可以写:
resource :accounts do
resource :tasks
end但这将以例如
/accounts/1/tasks希望有人能帮我写路由!
发布于 2013-02-24 17:29:39
现在我找到了正确的方法:
首先,我需要定义作用域,如下所示:
scope ":account_id" do
resources :tasks
resources :projects
end然后,为了让一切正常工作,在循环中创建链接,如下所示:
<%= link_to "Project", project %>将不起作用,您需要在应用程序控制器中设置默认url选项:
def default_url_options(options={})
if @current_account.present?
{ :account_id => @current_account.id }
else
{ :account_id => nil }
end
end它为我修复了所有的No Route Matches Error。如果没有:account_id,就不会有错误,例如,对于那些精巧的东西。
对于@Mohamad:
before_filter :set_current_account
# current account
def set_current_account
# get account by scoped :account_id
if params[:account_id]
@current_account = Account.find(params[:account_id])
return @current_account
end
# dont' raise the exception if we are in that devise stuff
if !devise_controller?
raise "Account not found."
end
end这种设计和错误处理可能会更好。:S
发布于 2013-02-14 03:32:37
你可以这样做一个作用域:
scope ":account_id" do
resources :tasks
resources :projects
endhttps://stackoverflow.com/questions/14861161
复制相似问题