简体   繁体   English

用户定义的路由

[英]User-defined routing

I have three relevant models: Companies, Projects, and Links, and I am trying to create a system where we generate routes according to the Links they create, for example: 我有三个相关的模型:公司,项目和链接,并且我试图创建一个系统,在该系统中我们根据它们创建的链接生成路线,例如:

www.site.com/the_company's_specific_path/one_of_company's_links

One company has_many :projects, one project has_many :links. 一家公司has_many:projects,一个项目has_many:links。

Summary of the structure: 结构摘要:

#Company table
company.id
company.path # such as 'Bechtel'

#Project table
project.id
project.company_id

#Link table
link.id
link.link # such as 'railwayproject'
link.project_id
link.company_id # some links may be tied to the company and not one specific project

#Final route generated: www.site.com/bechtel/railwayproject

How can I set up this system so: 如何设置此系统,以便:

  • Part 1 of the route specifies company.path (along the lines of @company = Company.find_by_path(params[:path]) ) 路线的第1部分指定company.path(沿@company = Company.find_by_path(params[:path])
  • Part 2 finds link (along the lines of @link = Link.find_by_link_and_company_id(params[:link],@company.id) ) 第2部分查找链接(沿@link = Link.find_by_link_and_company_id(params[:link],@company.id)
  • Upon entering this URI/URL the user enters 'show' where they see information on the project or company. 输入此URI / URL后,用户将输入“显示”,从中可以看到有关项目或公司的信息。 (When a link isn't tied specifically to a project we show 'list' instead, displaying all the company's projects.) (当链接未专门链接到项目时,我们将显示“列表”,以显示公司的所有项目。)

I apologize if for any reason the above is unclear. 如果上述原因不清楚,我们深表歉意。 I have tried to explain as best I can! 我已经尽力解释了! Thanks. 谢谢。

You should check out the documentation for ActionController::Routing , ActionController::Resources for RESTful routes, and a handy Rails Guide on the topic. 您应该查看用于RESTful路由的ActionController :: RoutingActionController :: Resources文档,以及有关该主题的便捷Rails指南 There are also a number of Railscasts regarding routes . 关于路线,也有许多Railscasts。

Something as simple as map.connect :path/:link, :controller => :companies would work, but it may cause some problems depending on your existing routes and is not RESTful. 诸如map.connect :path/:link, :controller => :companies这样简单的方法就可以使用,但是根据您现有的路由,它可能会导致一些问题,并且不是RESTful的。 If it does not cause any conflicts, this would allow you to do what you're asking for. 如果它不会引起任何冲突,那么这将使您可以执行所要求的操作。

You can add constraints to the route using the :requirements option to narrow down what is considered a match: 您可以使用:requirements选项将约束添加到路由,以缩小被认为是匹配项的范围:

map.company_link :company/:link, :requirements => { :company => /[\w\-]+/, :link => /[\w\-]+/ }

This would only match word and dash ('-') characters in the url, and I believe the default routes will still work properly. 这只会匹配网址中的单词和破折号('-')字符,我相信默认路由仍将正常工作。 I also made it a named route with map.company_link so Rails creates a set of url helpers allowing easy reference to your route: company_link_path(:company => "Bechtel", :link => "railwayproject") 我还使用map.company_link将其命名为路线,因此Rails创建了一组URL帮助器,可轻松引用您的路线: company_link_path(:company => "Bechtel", :link => "railwayproject")

If you want to stick with the REST way of doing this would be: 如果您要坚持使用REST的方式,那就是:

map.resource :companies do |company|
  company.resources :links
end

/companies/1/links/2 would get passed to the show action of the links_controller with the company and link ids as params. /companies/1/links/2将被传递给使用公司和链接ID作为参数的links_controller的show动作。 This is explained further in the nested resources section of the previously mentioned Rails Guide. 前面提到的《 Rails指南》的“ 嵌套资源”部分对此做了进一步说明。 Normally, you would use the :shallow => true option, because the link id of 2 is already unique and does not need to be nested under companies. 通常,您将使用:shallow => true选项,因为链接ID 2已经是唯一的,并且不需要嵌套在公司下。 The real value of the nested route is showing all of the links with /companies/1/links and all other actions going directly to the individual link /links/2 . 嵌套路由的真正价值在于显示所有带有/companies/1/links以及所有直接进入各个链接/links/2其他动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM