且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Rails 中渲染路由错误的 404 页面

更新时间:2023-11-10 23:08:28

代替

render not_found

你可以使用

render file: "#{Rails.root}/public/404.html" , status: 404

render file: "#{Rails.root}/public/404.html" , status: :not_found

更新

def destroy
  if current_user.username == @post.email 
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  else
    render file: "#{Rails.root}/public/404.html" , status: :not_found
  end
end  ## end is missing

更新 2

如果你想在 development 环境中显示 404 错误页面,那么请确保在 development.rb 文件中将以下设置为 false:

If you want to display 404 error page in development environment then make sure that the following is set to false in development.rb file:

  config.consider_all_requests_local       = false 

警告:这也意味着您不会在应用程序中看到任何错误(堆栈跟踪等).

WARNING: This also means that you would not see any errors raised on your application(stacktrace etc in view).