Parinha
Parinha

Reputation: 95

How do I redirect to 404 page when a user record does not exist?

e.g I have one model (User model). When user signsup for an account, there is an email sent to alert user that his/her account has been activated. In this case, if an admin deletes user record and then user clicks the link from the email to see his or her profile, it will show error. So, I want to check if a user record exists or not. if it does not exist, user should be redirected to 404 page.

I have tried the code below but it not working. this is the following example that i have tried

def show
  @user = User.find(params[:id]) or raise ActionController::RoutingError.new('Not Found')
end

So, is there a solution for this?

Thanks.

Upvotes: 3

Views: 6194

Answers (5)

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

It's quite simple, you just need to render rails default 404 page or your customized one..

In your application controller,

class ApplicationController < ActionController::Base
 # rest of your application controller code

 def content_not_found
   render file: "#{Rails.root}/public/404.html", layout: true, status: :not_found
 end
end

Then, call it from any controller you wish. In you case,

def show
  if (@user = User.find_by_id(params[:id]).present?
    # do your stuff
  else
    content_not_found
  end
end

I don't like exceptions, and I try to avoid them as much as possible ;)

Upvotes: 9

Divya Sharma
Divya Sharma

Reputation: 556

You can also make use of

Rails 3 and up

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

If you want to make use of application controller then you can try to write two points given below in application controller file:

1) create render_404 action:

  def render_404
    respond_to do |format|
      format.html { render file: "#{Rails.root}/public/404.html", status: 404 }
    end
  end

2) rescue_from ActiveRecord::RecordNotFound, :with => :render_404 action

As you are making use of 'show' action

def show
  @user = User.find_by(id: params[:id])
end

If no record found(no user found with params[:id]) then automatically there will be 404.

Upvotes: 0

Vamsi Krishna
Vamsi Krishna

Reputation: 3792

If you want at application level...

In application controller, add the following...

rescue_from ActionController::RoutingError, :with => :render_404

def render_404
  render :file => "#{Rails.root}/public/404.html",  :status => 404
end

In user Controller you can just say,

def show
  @user = User.find(params[:id])
end

If you want this just at single controller level,

In User controller, do the following

def show
  @user = User.find(params[:id])
rescue ActionController::RoutingError
  raise ActionController::RoutingError.new('Not Found')
end

Upvotes: 1

Osp
Osp

Reputation: 126

try this:

def show
  if (@user = User.find_by_id(params[:id])).present?
    @user = User.find(params[:id])
    authorize @user
  else
    raise ActionController::RoutingError.new('Not Found')
  end
end

Upvotes: 1

Gerry
Gerry

Reputation: 10497

Try this code instead:

def show
  @user = User.find_by(id: params[:id])

  raise ActionController::RoutingError.new('Not Found') if @user.blank?
end

Upvotes: 2

Related Questions