复杂的轨道路线

发布于 2024-08-17 10:35:49 字数 272 浏览 3 评论 0原文

我想做一些像 github 那样的嵌套 url 的事情,就像 如何路由用户配置文件 URL 以跳过控制器? 但不太确定如何继续操作。

例如,查看他们的提交:':user/:repo/commit/:sha',控制器正在提交。我将如何复制这种类型的嵌套资源?

谢谢 :)

I would like to do something like github has with nested urls, and like How do I route user profile URLs to skip the controller? but not really sure how to go on with it.

For example, looking at a commit they have: ':user/:repo/commit/:sha', with the controller being commit. How would I replicate this type of nested resource?

thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-08-24 10:35:49

如果 commit 是一个 RESTful 控制器,它使用 :sha 而不是 id 来查找记录。你可以这样做:

map.resource :commits, :path_prefix => ':user/:repo', :as => 'commit'

它将创建标准的 RESTful 路由,看起来像 http://yoursite.tld/:user/:repo/commit/:id

再说一次,如果你永远不会翻译url 中的 id 部分为提交 id,那么您没有理由不能将其用作 :sha 值。

示例:

class CommitController < ApplicationController
  def show
    @commit = Commit.find(:first, :conditions => {:sha => params[:id]})
  end
  ...
end

您可能还想覆盖提交模型中的 to_param 以返回 sha 值。

class Commt < ActiveRecord::Base
  ...
  def to_param
    sha
  end
end

这样,现在 link_to commit_url(@commit, :user => current_user, :repo => @repo) 将提供与您的方案匹配的 url。

If commit is a RESTful controller that uses :sha instead of an id to find records. You could do this instead:

map.resource :commits, :path_prefix => ':user/:repo', :as => 'commit'

It will create the standard RESTful routes that look like http://yoursite.tld/:user/:repo/commit/:id

Again, if you'll never be translating the id portion in the url to a commit id, then there's no reason you can't use it as a :sha value.

example:

class CommitController < ApplicationController
  def show
    @commit = Commit.find(:first, :conditions => {:sha => params[:id]})
  end
  ...
end

You may also want to over ride to_param in the commit model to return the sha value.

class Commt < ActiveRecord::Base
  ...
  def to_param
    sha
  end
end

So that now link_to commit_url(@commit, :user => current_user, :repo => @repo) will provide a url that matches your scheme.

雪落纷纷 2024-08-24 10:35:49

怎么样

map.connect ':user/:repo/commit/:sha', :action => :index

如果您需要 RESTful 路由,或者使用 map.resource 而不是 map.connect

。在控制器中,可以从 params 中检索 URL 信息,例如 params[:user] 返回用户名。

How about

map.connect ':user/:repo/commit/:sha', :action => :index

Or use map.resource instead of map.connect if you need a RESTful route.

In the controller, the URL information can be retrieved from params, for example params[:user] returns the username.

祁梦 2024-08-24 10:35:49

您可以根据需要命名路线,并指定要使用它们的控制器和操作。

例如,您可能有:

map.connect ':user/:repo/commit/:sha', :controller => 'transactions', :action => 'commit'

这会将请求发送到“事务”控制器中的“提交”方法。

然后可以使用参数在控制器中访问其他变量:

@user = User.find(params[:user])
@repo = Repo.find(params[:repo])

You can name your routes as you like, and specify which controllers and actions you'd like to use them with.

For example, you might have:

map.connect ':user/:repo/commit/:sha', :controller => 'transactions', :action => 'commit'

This would send the request to the 'commit' method in 'transactions' controller.

The other variables can then be accessed in your controller using params:

@user = User.find(params[:user])
@repo = Repo.find(params[:repo])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文