Rails Flash中的link_to() [英] link_to() in Rails flash
问题描述
当用户无法在我的Rails应用上登录时,我想将他们指向密码重设页面:
When a user fails login on my Rails app, I'd like to point them to a password reset page:
flash[:notice] = "Login failed. If you have forgotten your password, you can #{link_to('reset it', reset_path)}"
但是,我不能在控制器中使用link_to。不混合控制器和视图逻辑就可以做到这一点的最佳方法是什么?
However, I can't use link_to in a controller. What's the best way to do this without mixing controller and view logic?
我的最佳猜测是闪光灯是执行此操作的错误位置,但是我会感激任何人输入。
My best guess is that the flash is the wrong place to do this, but I'd appreciate any input.
推荐答案
我认为最常见的解决方案是在登录表单中粘贴指向密码重置页面的链接,因此您的Flash消息根本不需要处理。这也使用户可以在不先登录的情况下请求重设。
I think the most common solution is to stick a link to the password reset page right in your login form, so your flash message doesn't have to deal with it at all. That also allows the user to request the reset without first failing to log in.
如果要在即显消息中进行此操作,则应使用 url_for
构建链接,而不是 link_to
。
If you want to do it in the flash message, you should use url_for
to build the link instead of link_to
.
或者,您可以呈现部分而不是在控制器中对消息进行硬编码。
Alternatively, you could render a partial instead of hard-coding the message in your controller.
flash[:error] = render_to_string(:partial => "shared/login_failed_message")
# in shared/_login_failed_message.html.erb
<%= "Login failed. If you have forgotten your password, you can #{link_to('reset it', reset_path)}" %>
这篇关于Rails Flash中的link_to()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!