Rails link_to 带路径和 url_options
如何设置路径和 :path_only => Rails 的 link_to 方法为 false?
我们的网站有很多 Rails 制作的超链接,如下所示:
<% link_to "anchor text", "/path/" %>
其中产生 锚文本
我有任务用绝对 url 替换链接中的相对 url。自然地,我想到设置 :only_path => false
,但当我已经提供路径而不是 url_options 时,我不知道该怎么做。
是的,我可以指定其他 url_options(控制器、操作、id),但对于我正在工作的规模,使用路径比查找 id、操作和控制器要简单得多。
使用 Rails 2.3.5
How do I set a path and :path_only => false for Rails' link_to method?
Our site has a lot of rails-made hyperlinks looking like this:
<% link_to "anchor text", "/path/" %>
which yields <a href="/path/">anchor text</a>
I have the assignment to replace relative urls in our links with absolute urls. Naturally, it occurs to me to set :only_path => false
, but I don't know how to do that when I'm already supplying a path instead of url_options.
Yes, I could specify other url_options (controller, action, id), but for the scale on which I'm working, it's far simpler to use a path than to look up ids, actions, and controllers.
Using Rails 2.3.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议用命名的路由辅助函数替换硬编码路径。如果以前的程序员使用
path_path
而不是"/path/"
,那么您可以将_path
替换为_url
> 并完成它。如果您找到一种方法强制所有链接都是绝对的而不是相对的,那么下一个需要更改某些内容的人将与您现在处于同一条船上。即使替换路径的开销相对较高,这也是正确的方法。I would suggest replacing your hard-coded paths with the named route helper functions. If instead of
"/path/"
the previous programmer had usedpath_path
you would be able to replace_path
with_url
and be done with it. If you find a way to force all your links to be absolute instead of relative, the next person who needs to change something will be in the same boat as you are now. Even if there is a relatively high overhead for replacing paths, it is the right way to go.