views:

726

answers:

4

Hi,

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

+1  A: 

try this:

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

mgcm
A: 

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.

austinfromboston
A: 

Use link_to and button_to (in UrlHelper)

Dutow
A: 

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

Noel Walters