Przekierowanie do statycznej strony html w / public
Jak mogę przekierować /foo
do wyświetlania /public/foo.html
w Railach?
59
3 answers
Możesz to zrobić:
Dodaj to do swoich tras.plik rb.match '/foo', :to => redirect('/foo.html')
Update
W Rails 4 powinno się używać "get", a nie"match":get '/foo', :to => redirect('/foo.html')
Thanks Grant Birchmeier
94
Author: Arkan,
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-05-23 11:54:39
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-05-23 11:54:39
Można to zrobić bez wywoływania przekierowania. Wykonaj kolejne kroki w dół, aby móc trasować pliki statyczne w config/routes.rb
, Jak pokazano w tym przykładzie:
# This route will serve public/index.html at the /login URL
# path, and have a URL helper named `login_path`:
get "/login", to: static("index.html")
# This route will serve public/register.html at the /register
# URL path, and have URL helper named `new_user_registration_path`:
get "/register", to: static("register.html"), as: :new_user_registration
- Utwórz
config/initializers/static_router.rb
z całą zawartością pliku pokazaną na końcu tej odpowiedzi. Upewnij się, że przełączasz komentarze dla linii istotnych dla wersji aplikacji Rails. - Uruchom ponownie aplikację (najpierw
bin/spring stop
, Aby upewnić się, że aplikacja jest całkowicie przeładowana). - Zacznij używać metody
static(path)
w swoimconfig/routes.rb
.
# File: config/initializers/static_router.rb
module ActionDispatch
module Routing
class StaticResponder < Endpoint
attr_accessor :path, :file_handler
def initialize(path)
self.path = path
# Only if you're on Rails 5+:
self.file_handler = ActionDispatch::FileHandler.new(
Rails.configuration.paths["public"].first
)
# Only if you're on Rails 4.2:
# self.file_handler = ActionDispatch::FileHandler.new(
# Rails.configuration.paths["public"].first,
# Rails.configuration.static_cache_control
# )
end
def call(env)
env["PATH_INFO"] = @file_handler.match?(path)
@file_handler.call(env)
end
def inspect
"static('#{path}')"
end
end
class Mapper
def static(path)
StaticResponder.new(path)
end
end
end
end
8
Author: Eliot Sykes,
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-04-04 08:25:51
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-04-04 08:25:51
Np. w Rails 4 dodaj następującą trasę:
get '/example', :to => redirect('example.html')
Należy również włączyć pliki statyczne z katalogu 'public'w konfiguracji:
config.serve_static_files = true
Lub
config.serve_static_assets = true
Może być również konieczne podanie katalogu publicznego jako root w konfiguracji NGINX.
0
Author: Александр Тихонович,
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-03-05 15:52:48
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-03-05 15:52:48