代码之家  ›  专栏  ›  技术社区  ›  Marcos Crispino

重定向至Ruby on Rails中的XML

  •  0
  • Marcos Crispino  · 技术社区  · 14 年前

    我刚接触Ruby on Rails,所以可能我做错了什么…

    在一个控制器(用户)中,我有一个“before_filter”,它调用applicationcontroller类中的“authenticate”方法。

    用户控制器可以通过HTML或XML访问

    respond_to do |format|
      format.html
      format.xml
    end
    

    问题是当过滤器不通过时,authenticate方法总是显示HTML页面…

    我有两个文件authentication\u failed.html.erb和authentication\u failed.xml.erb。

    在控制器中,“验证失败”方法如下:

    def authentication_failed
      respond_to do |format|
        format
        format.xml
      end
    end
    

    authenticate方法(过滤器调用的方法)具有以下代码:

    if !(authenticated) then
      redirect_to authentication_failed_path
    end
    

    如果身份验证无法用XML而不是HTML页响应,我该怎么办?

    1 回复  |  直到 14 年前
        1
  •  0
  •   shingara    14 年前

    更好的方法是将格式重定向到

    if !(authenticated) then
      redirect_to authentication_failed_path(:format => params[:format]
    end
    

    现在,在重定向后保持相同的格式