Rails - 如何在视图中将路径转换为URL?

3
作为标题,在我的邮件视图中,我需要一个帮助程序来将路径转换为URL:
= link_to "Click Me", convert_to_urlh#with_hash")

我期望convert_to_url("/some_path#with_hash")生成http://localhost:3000/some_path#with_hash


你有查看文档吗?(https://www.omniref.com/ruby/gems/actionpack/4.1.6/symbols/ActionDispatch::Routing::UrlFor/url_for?d=137394105&n=0#tab=Docs&d=137394105&n=0)我不确定 url_for 是否像那样工作。只需执行 link_to "Click Me", "/some_path#with_hash" 即可完成。 - sebkkom
在链接中硬编码URL通常是不好的做法。您想要路由到哪个操作? - Marek Lipka
我需要在电子邮件中包含URL。我不应该在问题中使用url_for。我正在寻找一个辅助工具来实现这个目的。 - Trantor Liu
你想路由到哪个控制器/操作? - Marek Lipka
“users” 控制器和 “show” 操作。 - Trantor Liu
2个回答

0

这里不需要调用url_for,因为link_to方法已经使用第二个参数调用了它。请尝试这样做:

link_to 'Click Me', '/some_path#with_hash'

0
如果您想在ActionMailer中获取URL,则需要指定:host。您可以使用普通的url_forpolymorphic_url或生成的命名路由方法。

例如:

<%= link_to 'Click me', user_url(@user, host: "example.com") %>

在 Rails 的视图和控制器中,:host 会自动替换。
更多信息:
- 关于 AM:http://api.rubyonrails.org/classes/ActionMailer/Base.html - 关于 url_forhttp://api.rubyonrails.org/classes/ActionDispatch/Routing/UrlFor.html - 关于 polymorphic_urlhttp://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接