Jak przekierować na 404 w Rails?
Chciałbym "sfałszować" stronę 404 w Rails. W PHP po prostu wysłałbym nagłówek z kodem błędu jako takim:
header("HTTP/1.0 404 Not Found");
Jak to się robi z Rails?
9 answers
Nie Renderuj 404 samemu, nie ma ku temu powodu; Rails ma już wbudowaną tę funkcjonalność. Jeśli chcesz wyświetlić stronę 404, Utwórz metodę render_404
(lub not_found
jak ją nazwałem) w ApplicationController
w następujący sposób:
def not_found
raise ActionController::RoutingError.new('Not Found')
end
Rails również obsługuje AbstractController::ActionNotFound
i ActiveRecord::RecordNotFound
w ten sam sposób.
To robi dwie rzeczy lepiej:
1) używa wbudowanego w rescue_from
Handlera Rails do renderowania strony 404 i
2) przerywa wykonywanie kodu, pozwalając ci robić miłe rzeczy like:
user = User.find_by_email(params[:email]) or not_found
user.do_something!
Bez konieczności pisania brzydkich instrukcji warunkowych.
Jako bonus, jest również bardzo łatwy w obsłudze w testach. Na przykład w teście integracji rspec:# RSpec 1
lambda {
visit '/something/you/want/to/404'
}.should raise_error(ActionController::RoutingError)
# RSpec 2+
expect {
get '/something/you/want/to/404'
}.to raise_error(ActionController::RoutingError)
I minitest:
assert_raises(ActionController::RoutingError) do
get '/something/you/want/to/404'
end
Lub zobacz więcej informacji z Rails render 404 not found z akcji kontrolera
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-27 10:46:37
HTTP 404 Status
Aby zwrócić nagłówek 404, po prostu użyj opcji :status
dla metody render.
def action
# here the code
render :status => 404
end
Jeśli chcesz renderować standardową stronę 404, możesz wyodrębnić tę funkcję w metodzie.
def render_404
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
I wywołaj to w swojej akcji
def action
# here the code
render_404
end
Jeśli chcesz, aby akcja renderowała stronę błędu i zatrzymała, po prostu użyj instrukcji return.
def action
render_404 and return if params[:something].blank?
# here the code that will never be executed
end
ActiveRecord i HTTP 404
Pamiętaj również, że Rails ratuje niektóre błędy ActiveRecord, takie jak ActiveRecord::RecordNotFound
wyświetlanie strony błędu 404.
To znaczy, że nie musisz sam ratować tej akcji
def show
user = User.find(params[:id])
end
User.find
wyświetla ActiveRecord::RecordNotFound
, gdy użytkownik nie istnieje. Jest to bardzo potężna funkcja. Spójrz na następujący kod
def show
user = User.find_by_email(params[:email]) or raise("not found")
# ...
end
Możesz to uprościć delegując do Rails czek. Wystarczy użyć wersji bang.
def show
user = User.find_by_email!(params[:email])
# ...
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-31 09:48:48
Nowo wybrana odpowiedź Stevena Soroki jest bliska, ale nie kompletna. Sam test ukrywa fakt, że to nie zwraca prawdziwego 404 - to zwraca status 200 - "sukces". Oryginalna odpowiedź była bliższa, ale próbowała renderować układ tak, jakby nie doszło do żadnej awarii. To naprawia wszystko:
render :text => 'Not Found', :status => '404'
Oto mój typowy zestaw testowy dla czegoś, czego spodziewam się zwrócić 404, używając RSpec i Shoulda matchers:
describe "user view" do
before do
get :show, :id => 'nonsense'
end
it { should_not assign_to :user }
it { should respond_with :not_found }
it { should respond_with_content_type :html }
it { should_not render_template :show }
it { should_not render_with_layout }
it { should_not set_the_flash }
end
Ta zdrowa paranoja pozwoliła ja do zauważenia niedopasowania typu treści, gdy Wszystko inne wyglądało pięknie :) sprawdzam wszystkie te elementy: przypisane zmienne, kod odpowiedzi, typ treści odpowiedzi, renderowany szablon, renderowany układ, wiadomości flash.
Pominę sprawdzanie typu zawartości w aplikacjach, które są ściśle html...czasami. Przecież "sceptyk sprawdza wszystkie szuflady":)
Http://dilbert.com/strips/comic/1998-01-20/
FYI: nie polecam testowania rzeczy, które są dzieje się w kontrolerze, czyli "should_raise". Zależy ci na wydajności. Moje powyższe testy pozwoliły mi wypróbować różne rozwiązania, a testy pozostają takie same, czy rozwiązaniem jest wywołanie wyjątku, specjalny rendering itp.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-08-17 20:58:17
Możesz również użyć pliku renderującego:
render file: "#{Rails.root}/public/404.html", layout: false, status: 404
Gdzie można wybrać użycie układu lub nie.
Inną opcją jest użycie WYJĄTKÓW do kontrolowania go:
raise ActiveRecord::RecordNotFound, "Record not found."
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-19 15:41:52
Wybrana odpowiedź nie działa w Rails 3.1+, ponieważ obsługa błędów została przeniesiona do oprogramowania pośredniczącego (zobacz problem z githubem).
Oto rozwiązanie, które znalazłem, z którego jestem całkiem zadowolony.
W ApplicationController
:
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: :handle_exception
end
def not_found
raise ActionController::RoutingError.new('Not Found')
end
def handle_exception(exception=nil)
if exception
logger = Logger.new(STDOUT)
logger.debug "Exception Message: #{exception.message} \n"
logger.debug "Exception Class: #{exception.class} \n"
logger.debug "Exception Backtrace: \n"
logger.debug exception.backtrace.join("\n")
if [ActionController::RoutingError, ActionController::UnknownController, ActionController::UnknownAction].include?(exception.class)
return render_404
else
return render_500
end
end
end
def render_404
respond_to do |format|
format.html { render template: 'errors/not_found', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
def render_500
respond_to do |format|
format.html { render template: 'errors/internal_server_error', layout: 'layouts/application', status: 500 }
format.all { render nothing: true, status: 500}
end
end
I w application.rb
:
config.after_initialize do |app|
app.routes.append{ match '*a', :to => 'application#not_found' } unless config.consider_all_requests_local
end
I w moich zasobach (Pokaż, edytuj, Aktualizuj, Usuń):
@resource = Resource.find(params[:id]) or not_found
Z pewnością można by to poprawić, ale przynajmniej mam różne widoki dla not_found i internal_error bez nadpisywania funkcji Core Rails.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-31 09:49:36
To ci pomoże...
Kontroler Aplikacji
class ApplicationController < ActionController::Base
protect_from_forgery
unless Rails.application.config.consider_all_requests_local
rescue_from ActionController::RoutingError, ActionController::UnknownController, ::AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
end
private
def render_error(status, exception)
Rails.logger.error status.to_s + " " + exception.message.to_s
Rails.logger.error exception.backtrace.join("\n")
respond_to do |format|
format.html { render template: "errors/error_#{status}",status: status }
format.all { render nothing: true, status: status }
end
end
end
Kontroler błędów
class ErrorsController < ApplicationController
def error_404
@not_found_path = params[:not_found]
end
end
Views / errors / error_404.html.haml
.site
.services-page
.error-template
%h1
Oops!
%h2
404 Not Found
.error-details
Sorry, an error has occured, Requested page not found!
You tried to access '#{@not_found_path}', which is not a valid page.
.error-actions
%a.button_simple_orange.btn.btn-primary.btn-lg{href: root_path}
%span.glyphicon.glyphicon-home
Take Me Home
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-10 11:51:13
<%= render file: 'public/404', status: 404, formats: [:html] %>
Po prostu dodaj to do strony, którą chcesz renderować na stronie błędu 404 i gotowe.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-01 06:42:03
Aby przetestować obsługę błędów, możesz zrobić coś takiego:
feature ErrorHandling do
before do
Rails.application.config.consider_all_requests_local = false
Rails.application.config.action_dispatch.show_exceptions = true
end
scenario 'renders not_found template' do
visit '/blah'
expect(page).to have_content "The page you were looking for doesn't exist."
end
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-14 20:40:50
Jeśli chcesz obsługiwać różne 404 na różne sposoby, rozważ złapanie ich w kontrolerach. To pozwoli Ci robić rzeczy takie jak śledzenie liczby 404S generowanych przez różne grupy użytkowników, mieć wsparcie interakcji z użytkownikami, aby dowiedzieć się, co poszło nie tak / jaka część doświadczenia użytkownika może wymagać poprawek, zrobić testy A / B itp.
Umieściłem tutaj logikę bazową w ApplicationController, ale można ją również umieścić w bardziej konkretnych kontrolerach, aby mieć specjalną logikę tylko dla jednego kontrolera.
Powodem, dla którego używam if z ENV['RESCUE_404'], jest to, że mogę przetestować podnoszenie ar::RecordNotFound w izolacji. W testach mogę ustawić ten ENV var na false, a mój rescue_from nie odpali. W ten sposób mogę przetestować podniesienie oddzielnie od warunkowej logiki 404.
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :conditional_404_redirect if ENV['RESCUE_404']
private
def conditional_404_redirect
track_404(@current_user)
if @current_user.present?
redirect_to_user_home
else
redirect_to_front
end
end
end
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-25 07:15:19