代码之家  ›  专栏  ›  技术社区  ›  Grandpa

在Rails Flash中链接到()。

  •  10
  • Grandpa  · 技术社区  · 16 年前

    当用户在我的Rails应用程序上登录失败时,我想将其指向密码重置页面:

    flash[:notice] = "Login failed.  If you have forgotten your password, you can #{link_to('reset it', reset_path)}"
    

    但是,我不能在控制器中使用Link_to。在不混合控制器和视图逻辑的情况下,最好的方法是什么?

    我的最佳猜测是闪光灯是做这件事的错误地方,但我会感谢任何输入。

    3 回复  |  直到 12 年前
        1
  •  10
  •   Baldu    16 年前

    我认为最常见的解决方案是在你的登录表单中插入一个指向密码重置页面的链接,这样你的flash消息就不需要处理它了。这还允许用户请求重置,而不会先登录失败。

    如果你想在快闪信息中这样做,你应该使用 url_for 构建链接而不是 link_to .

    或者,可以呈现部分消息,而不是在控制器中对消息进行硬编码。

    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)}" %>
    
        2
  •  5
  •   Daniel Fischer    16 年前
    flash[:notice] = "Login failed.  If you have forgotten your password, you can <a href='#{url_for(reset_path)}'>reset it</a>"
    

    正确,链接到是视图助手。请给我们一个更通用的链接构建方式,网址

        3
  •  5
  •   Jan    14 年前

    今天,这个问题的最佳答案可能是 http://www.railsexperiments.com/using-helpers-inside-controllers )

    flash[:notice] = "Login failed.  If you have forgotten your password, you can #{view_context.link_to('reset it', reset_path)}".html_safe