Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

ruby on rails - How redirect to 404 page in routes.rb?

How can I redirect incorrect url to 404 page in routes.rb? Now I use 2 examples code:

# example 1
match "/go/(*url)", to: redirect { |params, request| Addressable::URI.heuristic_parse(params[:url]).to_s }, as: :redirect, format: false

# example 2
match "/go/(*url)", to: redirect { |params, request| Addressable::URI.heuristic_parse(URI.encode(params[:url])).to_s }, as: :redirect, format: false

But when I try using russian words in 'url' parameter, in 1st example I get 500 page (bad URI), in 2nd - I get redirect to stage.example.xn--org-yedaaa1fbbb/

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want custom error pages, you'll be best looking at this answer I wrote a few weeks ago


You need several important elements to create custom error routes:

-> Add custom error handler in application.rb:

# File: config/application.rb
config.exceptions_app = self.routes

-> Create /404 routes in your routes.rb:

# File: config/routes.rb
if Rails.env.production?
   get '404', :to => 'application#page_not_found'
end

-> Add actions to application controller to handle these routes

# File: app/controllers/application_controller.rb
def page_not_found
    respond_to do |format|
      format.html { render template: 'errors/not_found_error', layout: 'layouts/application', status: 404 }
      format.all  { render nothing: true, status: 404 }
    end
  end

This is obviously relatively basic, but hopefully it will give you some more ideas on what you can do


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...