【问题标题】:Redirect rails app to home page on crash崩溃时将rails应用程序重定向到主页
【发布时间】:2013-09-27 06:36:09
【问题描述】:

我希望我的应用重定向到我的主页,即帖子#index。这是一个 rails2 应用程序,我正在尝试迁移到 rails 3。

def rescue_action_in_public(exception)
  flash[:notice] = "There was an error. Please try again." #  #{exception}
  redirect_to :controller => :posts, :action => :index
end

我认为这种方法可以完成这项任务。但是,它在 Rails 3 中不起作用,我看到“对不起,出了点问题!”页面

我怎样才能让这个功能在 rails 3 中工作?如果需要更多信息,我愿意在这里粘贴。

【问题讨论】:

    标签: ruby-on-rails-3 ruby-on-rails-2


    【解决方案1】:

    在 Rails 3 中试试这个

    def rescue_action_in_public(exception)
            status = status_code(exception)
            locale_path = "#{public_path}/#{status}.#{I18n.locale}.html" if I18n.locale
            path = "#{public_path}/#{status}.html"
    
            if locale_path && File.exist?(locale_path)
              render(status, File.read(locale_path))
            elsif File.exist?(path)
              render(status, File.read(path))
            else
              render(status, '')
            end
          end
    

    来自apidock

    【讨论】:

      【解决方案2】:

      你可以这样!

      def rescue_action_in_public(exception)
        flash[:notice] = "There was an error. Please try again." #  #{exception}
        redirect_to posts_path
      end
      

      【讨论】:

        猜你喜欢
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多