0

At the end of my routes.rb, I have a wildcard match for vanity urls. However, I can't figure out how to redirect unknown usernames to a 404. How can you do this?

~ in routes.rb ~

# Vanity URLs
match ':username', :to => "users#show"

~ in the users controller ~

@user = User.find_by_username(params[:username])
if @user.nil?
  render :status => 404
else
  ...
end
Geoff
  • 9,470
  • 13
  • 52
  • 67
  • What part of this isn't working for you? – Chris Cherry Jun 11 '11 at 23:30
  • It just didn't redirect at all. As jdeseno answered, it was still rendering the default template with a 404 response code. You have to use the code jdeseno provided. – Geoff Jun 12 '11 at 04:44

1 Answers1

0

What you have will still render the default template but, with a 404 response code.

Try this to render the default 404 page instead:

render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
jdeseno
  • 7,753
  • 1
  • 36
  • 34