[英]In Ruby on Rails, how can you use vanity URLs and also redirect to 404s?
At the end of my routes.rb, I have a wildcard match for vanity urls.在我的 routes.rb 末尾,我有一个虚 url 的通配符匹配。 However, I can't figure out how to redirect unknown usernames to a 404. How can you do this?
但是,我不知道如何将未知用户名重定向到 404。你怎么能这样做?
~ in routes.rb ~ ~ 在 routes.rb 中 ~
# Vanity URLs
match ':username', :to => "users#show"
~ in the users controller ~ ~ 在用户 controller ~
@user = User.find_by_username(params[:username])
if @user.nil?
render :status => 404
else
...
end
What you have will still render the default template but, with a 404 response code.您所拥有的仍将呈现默认模板,但带有 404 响应代码。
Try this to render the default 404 page instead:试试这个来呈现默认的 404 页面:
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.