Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

render a 404 page on routing error in rails

I'm trying to render the integrated 404 page in rails as an exception. I tried this but still getting the routing error page:

posts_controller.rb

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
  not_found
end

application_controller.rb

 def not_found
  raise ActionController::RoutingError.new('Not Found')
end 

routes.rb

 Booklist::Application.routes.draw do
 get "pages/faq"
 get "pages/about"
 devise_for :users
 resources :posts

 root 'posts#index'
 end

view:

<% if current_user.username == post.email %>
  <font color="red">This is your post! Feel free to edit or delete it. ->  </font>
    <%= link_to 'Edit', edit_post_path(post) %>
    <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>
    <% end %>

No route matches [GET] "/posts/15/destroy"

like image 753
franklinexpress Avatar asked Mar 30 '14 19:03

franklinexpress


People also ask

How do I return a 404 in Rails?

To return a 404 header, just use the :status option for the render method. If you want to render the standard 404 page you can extract the feature in a method. If you want the action to render the error page and stop, simply use a return statement.

How do I trigger a 404 error?

One typical trigger for an error 404 message is when the page has been deleted from the website. The page was moved to another URL and the redirection was done incorrectly. You entered an incorrect URL address. Although it happens very rarely, sometimes the server malfunctions.

What is routes RB in Rails?

rb . The Rails router recognises URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. Let's consider an application to book rooms in different Hotels and take a look at how this works.


2 Answers

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

like image 162
Kirti Thorat Avatar answered Sep 20 '22 02:09

Kirti Thorat


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
like image 25
Robert Krzyzanowski Avatar answered Sep 18 '22 02:09

Robert Krzyzanowski