简体   繁体   English

没有路线的Rails路径助手

[英]Rails path helpers without routes

We are in the process of taking a few pages out of our rails app to be served separately (they are a few static pages with some content being managed through a cms). 我们正在从Rails应用程序中取出一些页面进行单独处理(它们是一些静态页面,其中一些内容是通过cms管理的)。 The urls will stay the same. 网址将保持不变。 Our own routing system in front of the servers will decide which request should go to the rails app and which to the static part. 服务器前面的我们自己的路由系统将决定哪个请求应该发送到rails应用程序,哪个请求应该发送到静态部分。

My question is about path helpers that we use quite a bit throughout the rails app, such as link_to about_path that generate mahwebsite.com/about . 我的问题是关于在整个Rails应用程序中mahwebsite.com/about使用的路径助手,例如生成mahwebsite.com/about link_to about_path As I understand I can just leave them be, they will still generate correct urls. 据我了解,我可以不理会它们,它们仍然会生成正确的网址。 My only concern is that for them to work I'll have to keep the routings in the routes file, which will have to be connected to the dummy controller methods. 我唯一关心的是,要使它们起作用,我必须将路由保留在路由文件中,该文件必须连接到虚拟控制器方法。 Seems like a lot of redundant code just to fool rails into creating path helpers. 似乎有很多冗余代码只是为了欺骗导轨创建路径助手。

Alternatively, I can hard-code links to the static pages. 另外,我可以硬编码到静态页面的链接。 But before I start replacing a whole lot of code, I'd like to know if there is a clean Railsy way to keep the path helpers without having to route to the redundant controllers. 但是在开始替换大量代码之前,我想知道是否存在一种干净的Railsy方法来保留路径助手,而不必路由到冗余控制器。

Thanks. 谢谢。

How about 怎么样

resources :custom_pages, only: [:your_options] do
  get :view/:page_id_or_whatever_for_identify
end

and do the following content with the controller? 控制器的以下内容?

Why not just create your own helper method? 为什么不只是创建自己的帮助器方法? EG 例如

# application_controller.rb
def about_path
  "mahwebsite.com/about"
end
helper_method :about_path
alias_method :about_url, :about_path

This will overwrite any Rails helper method and do exactly what you're after :) 这将覆盖任何Rails助手方法,并完全按照您的意愿进行:)

Hope this helps - give me a shout if you've any questions or comments. 希望这会有所帮助-如果您有任何疑问或意见,请给我喊。

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

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