当用户在我的Rails应用程序上登录失败时,我想将他们指向密码重置页面:
flash[:notice] = "Login failed. If you have forgotten your password, you can #{link_to('reset it', reset_path)}"
但是,我不能在 Controller 中使用link_to。没有混合 Controller 和 View 逻辑的最佳方法是什么?
我最好的猜测是闪光灯是执行此操作的错误位置,但是我很感谢您的投入。
最佳答案
我认为最常见的解决方案是在登录表单中直接添加一个指向密码重置页面的链接,因此您的Flash消息根本不需要处理。这也使用户可以在不首先登录的情况下请求重设。
如果要在即时消息中执行此操作,则应使用url_for
而不是link_to
来构建链接。
或者,您可以呈现部分消息,而不是在 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)}" %>
关于ruby-on-rails - Rails Flash中的link_to(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1598150/