Sam Kong
Sam Kong

Reputation: 5840

rails redirect_to problem

I am developing a rails app and have a question.

In my routes.rb:

map.connect 'admin', :controller => "/admin/users", :action => "index"

So when I go to "http://mydomain.com/admin", it redirects to "http://mydomain.com/admin/users/index". However, the address remains as "http://mydomain.com/admin". Thus, links in the page are wrong because they are created based on "http://mydomain.com/admin".

What's the solution to this problem?

Sam

Upvotes: 0

Views: 1304

Answers (4)

Noel Walters
Noel Walters

Reputation: 1853

Your code is not redirecting the browser it's just setting up /admin and /admin/users to trigger the same action.

You could try:

map.connect 'admin', :controller => "/admin/users", :action => "redirect_to_index"

Then in your controller write:

def redirect_to_index
  redirect_to :action => :index
end

This will send a redirect to the browser, causing it to display the correct URL.

Hopefully there is a better method that only involves routes.rb. This site might be helpful -> redirect-routing-plugin-for-rails

Upvotes: 0

Dutow
Dutow

Reputation: 5668

Use link_to and button_to (in UrlHelper)

Upvotes: 0

austinfromboston
austinfromboston

Reputation: 3780

Make sure any same-domain links on the page start with a /, and use the full path. Generally you should use Rails route methods to generate your links when possible. Same goes for using the image_tag and stylesheet_link_tag helpers.

So if you have a link to "privacy.html", change it to "/privacy.html" and you should be all good no matter where in the route structure you are. This is extra nice when you start extracting your view code out to re-usable partials.

Upvotes: 0

mgcm
mgcm

Reputation: 125

try this:

map.connect 'admin/:action/:id', :controller => 'admin/users'

Upvotes: 1

Related Questions