render a 404 page on routing error in rails

限于喜欢 提交于 2019-12-06 11:44:28

Instead of

render not_found

you could use

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

Or

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

UPDATE

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

UPDATE 2

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).

You need to place not_found in ApplicationController, not in PostsController, and then you don't need to render it, you can just call it like

# your code above...
else
  not_found
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!