-
Notifications
You must be signed in to change notification settings - Fork 21.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redirects in engine routes works strange #7977
Comments
Does the following working in a plain application Rails.application.routes.draw do
namespace :pages do
namespace :admin
root to: redirect("/admin/map")
resource :map
end
end
end I haven't checked but I think |
@pixeltrix, yes, it redirects to /admin/map. |
The following should work for engine routes: Pages::Engine.routes.draw do
namespace :admin do
root to: redirect do |params, request|
"#{request.script_name}/admin/map"
end
resource :map
end
end There's an argument that it should automatically prepend the /cc @tenderlove @rafaelfranca @josevalim what do you think? Should we automatically prepend the |
Rails 4 is on the way, what is a decision about it? |
@pixeltrix, could you shed some light? |
@pixeltrix if we automatically prepend the SCRIPT_NAME how will be the block code to remove it? |
@rafaelfranca that's the problem - I've got the code and tests to fix this but can't think of a good way to break out of the |
Any progress on how to remove the |
Although given Rails 4 has shipped, is this now even feasible to change? |
@JonRowe it'd be feasible for merging to master but I still don't have a good way of redirecting to a url outside of One possible solution is to support relative paths so if the redirect returns a url starting with '/' it's treated as an absolute url and 'path/to/action' would have the |
I think it's kind of counter intuitive to not take account of script_name (even though it's consistent), simply because an engine is designed to be mounted on another endpoint and thus is most likly to want consistent paths relative to it's own root. However I like the idea of |
@rafaelfranca wdyt? By using paths starting with a slash to indicate an absolute path then it should be backwards compatible. |
@pixeltrix I like this approach. 👍 from my side |
Example: # application routes.rb mount BlogEngine => '/blog' # engine routes.rb get '/admin' => redirect('admin/dashboard') This now redirects to the path `/blog/admin/dashboard`, whereas before it would've generated an invalid url because there would be no slash between the host name and the path. It also allows redirects to work where the application is deployed to a subdirectory of a website. Fixes #7977 (cherry picked from commit 9dbd208) Conflicts: actionpack/CHANGELOG.md
How are we supposed to redirect_to engine root path ? I've tried |
I would guess that |
I think we need to change
in https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/redirection.rb#L58 I tried this out, but I got 1 failing test(i.e test_redirect_hash_with_domain_and_path) where expected => "http://www.example-documentation.com" and actual => "http://www.example-documentation.com/" Should that be a problem?? @pixeltrix |
Example: # application routes.rb mount BlogEngine => '/blog' # engine routes.rb get '/welcome' => redirect('') This now redirects to the path `/blog`, whereas before it would redirect to the application root path. In the case of a path redirect or a custom redirect if the path returned contains a host then the path is treated as absolute. Similarly for option redirects, if the options hash returned contains a `:host` or `:domain` key then the path is treated as absolute. Fixes #7977 (cherry picked from commit b64bac4)
For anyone arriving via Google, the correct code to do this in Rails 3 is root(to: redirect do |params, request|
"#{request.script_name}/admin/map"
end) (note the necessary parentheses), or root to: redirect { |params, request| "#{request.script_name}/admin/map" } which I find much cleaner. |
The editor uses '/services/:service_id/preview' as base url. The problem redirecting to '/text-first' is that this url doesn't exist on the editor (only '/services/:service_id/preview/text-first') Add a suggestion from rails/rails#7977 makes work as expected also using request.env['PATH_INFO'] works instead of request.path (which was '/services/:service_id/preview/text-first')
There are some caveats of using the request.referer: The editor uses '/services/:service_id/preview' as base url. The problem redirecting to '/text-first' is that this url doesn't exist on the editor (only '/services/:service_id/preview/text-first') Adding he request.script_name as suggested here: rails/rails#7977 makes the redirection to work as expected Also using request.env['PATH_INFO'] (which is 'text-first' works instead of request.path (which was '/services/:service_id/preview/text-first') I tried to add a current page as hidden field but also has some drawbacks so I rollback. Remove spring: This is not need on the dummy app because we don't need t o dummy app to be quick reloading as we don't often change the app, only the gem Now the post request expect a page.url which is the current page. Example: You are on the first page and submit the form: The app will make a POST /reserved/first-page/answers and then redirect to /second-page. Then when you are on the second page and submit the form: The app will make a POST /reserved/second-page/answers and then redirect to next page.
Assuming engine routes:
And an application:
GET "/pages/admin" should redirect to "/pages/admin/map", but redirecting to "/admin/map"
This works in plain rails apps, thus I think, that engines should behave the same way.
The text was updated successfully, but these errors were encountered: