基于上下文的Rails 3路由 [英] Rails 3 routing based on context

查看:92
本文介绍了基于上下文的Rails 3路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个类似于GitHub使用的上下文系统。例如,可以根据属于用户上下文还是引用该公司之一的上下文来创建属于用户或用户所属公司之一的帖子。



作为其中的一部分,我希望能够根据用户当前的上下文进行路由。例如,如果用户位于自己的上下文中,则 / dashboard 应该路由到 users / show ,但是如果他们在ID为35的公司的上下文中,则 / dashboard 应该路由到 companies / 35 / dashboard



我可以将 /仪表板路由到负责做出此类决定的特殊控制器,例如 context #dashboard 然后可以执行 redirect_to ,但这感觉不太正确(也许是因为我们采用了Rails路由模块的逻辑



在Rails 3中解决此问题的正确方法是什么?

解决方案

我终于找到了解决我喜欢的问题的方法。



首先,假设一个会话存储的 Context 对象存储了是否用户处于用户上下文或公司上下文中。如果用户在公司上下文中,则对象所在的公司所在的ID也是如此。我们可以通过名为 get_context 的助手获取上下文,并可以通过 current_user 获取当前登录的用户。 / p>

现在,我们将路由设置为:



config / routes.rb

  MyApplication :: Application.routes.draw do 
get dashboard = > redirect,:user => / users / show,:company => / companies /:id / dashboard
结尾

现在, app / controllers / redirect_controller.rb

  class RedirectController< ApplicationController 
def method_missing(方法,* args)
user_url = params [:user]
company_url = params [:company]
context = get_context

case context.type
当:user
redirect_to user_url.gsub(:id,current_user.id.to_s)
when:company
redirect_to company_url.gsub(:id ,context.id.to_s)
结束
结束
结束

将重定向的实际URL保留在它们所属的位置很容易(在 routes.rb 文件中!),然后将数据传递到DRY控制器中。我什至可以在路由中传递当前上下文对象的ID。


I am trying to implement a "context" system similar to the one used by GitHub. For example, a Post may be created belonging either to the User or one of the Companies the User belongs to depending on whether to User is in the "User" context or a context that refers to one of the Companies.

As a part of this, I'd like to be able to do routing based on the user's current context. For example, if the User is in their own context, /dashboard should route to users/show, but if they are in the context for Company with ID 35, then /dashboard should route to companies/35/dashboard.

I could route /dashboard to a special controller responsible for making such decisions, such as context#dashboard which could then do a redirect_to, but this doesn't feel quite right (perhaps because we're taking logic that the Rails routing module should be responsible for and moving it to a controller?)

What would be the proper way to solve this problem in Rails 3?

解决方案

I finally found a solution to my problem that I like. This will use the URLs from my original question.

First, assume a session-stored Context object that stores whether the user is in a "user" context or a "company" context. If the user is in a "company" context, then the ID of the company they're working as is in the object as well. We can get the context via a helper named get_context and we can get the currently logged-in user via current_user.

Now, we set up our routes as so:

config/routes.rb:

MyApplication::Application.routes.draw do
  get "dashboard" => "redirect", :user => "/users/show", :company => "/companies/:id/dashboard"
end

Now, app/controllers/redirect_controller.rb:

class RedirectController < ApplicationController
  def method_missing(method, *args)
    user_url    = params[:user]
    company_url = params[:company]
    context     = get_context

    case context.type
    when :user
      redirect_to user_url.gsub(":id", current_user.id.to_s)
    when :company
      redirect_to company_url.gsub(":id", context.id.to_s)
    end
  end
end

It's easy enough to keep the actual URLs for the redirect where they belong (in the routes.rb file!) and that data is passed in to a DRY controller. I can even pass in the ID of the current context object in the route.

这篇关于基于上下文的Rails 3路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆