Ruby-on-rails Rails 中的路由别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4503550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:57:26  来源:igfitidea点击:

Route alias in Rails

ruby-on-rails

提问by Victor

I have a model stories in Rails 3.

我在 Rails 3 中有一个模型故事。

I want to make an alias "books" for "stories" so I can have routes /books/192instead of /stories/192, and also that all my generated links (e.g. link_to) point to books' routes instead of stories' routes.

我想为“故事”创建一个别名“书”,这样我就可以有路线/books/192而不是/stories/192,而且我生成的所有链接(例如link_to)都指向书的路线而不是故事的路线。

How can I do that?

我怎样才能做到这一点?

Thanks

谢谢

回答by Ryan

resources :stories, :path => :books

resources :stories, :path => :books

If you want to rename the path ANDhelper methods, then you do:

如果要重命名路径辅助方法,请执行以下操作:

resources :stories, :path => :books, :as => :books

resources :stories, :path => :books, :as => :books

See: Overriding the Named Helpers

请参阅:覆盖命名助手

回答by Ryan Bigg

That's why they made the pathoption on matchwhich is also available on resources:

这就是为什么他们作出path的选择match,其也可在resources

resources :stories, :path => "books"

回答by jschorr

Try something like this:

尝试这样的事情:

match 'books/:id' => 'books#show'
match 'books' => 'books#index'