DynastySS
DynastySS

Reputation: 395

Rails routing issues and questions

I have been trying to read the rails routing from outside in and failing miserably. I believe what I want to do is pretty basic, but I for whatever reason can't figure it out. Here is the situation.

I want my urls to read www.example.com/maps instead of what they are doing now www.example.com/maps/map which is the controller then view names.

My routes look like the following.

homepages GET    /homepages(.:format)                   homepages#index
              POST   /homepages(.:format)                   homepages#create
 new_homepage GET    /homepages/new(.:format)               homepages#new
edit_homepage GET    /homepages/:id/edit(.:format)          homepages#edit
     homepage GET    /homepages/:id(.:format)               homepages#show
              PUT    /homepages/:id(.:format)               homepages#update
              DELETE /homepages/:id(.:format)               homepages#destroy
         root        /                                      homepages#index
         maps GET    /maps(.:format)                        maps#index
              POST   /maps(.:format)                        maps#create
      new_map GET    /maps/new(.:format)                    maps#new
     edit_map GET    /maps/:id/edit(.:format)               maps#edit
          map GET    /maps/:id(.:format)                    maps#show
              PUT    /maps/:id(.:format)                    maps#update
              DELETE /maps/:id(.:format)                    maps#destroy
   work_index GET    /work(.:format)                        work#index
              POST   /work(.:format)                        work#create
     new_work GET    /work/new(.:format)                    work#new
    edit_work GET    /work/:id/edit(.:format)               work#edit
         work GET    /work/:id(.:format)                    work#show
              PUT    /work/:id(.:format)                    work#update
              DELETE /work/:id(.:format)                    work#destroy
contact_index GET    /contact(.:format)                     contact#index

I tried doing something like resources :maps, :path => '' but when I visited my site the url appeared the same. Is that due to the fact the code on my view looks like

<li class="current_page_item"><%= link_to "Bio", :controller => :homepages, :action => :index %></li>
                <li><%= link_to "Work", :controller => :work, :action => :experience %></li>
                <li><%= link_to "Map", :controller => :maps, :action => :map %></li>
                <li><%= link_to "Contact", :controller => :contact, :action => :email %></li>

Or is that because my routes are wrong?

My route file currently looks like

Me::Application.routes.draw do
  resources :homepages
  root :to => 'homepages#index'

  resources :maps

  resources :work

  resources :contact

  resources :media

Thanks for the assistance!

Upvotes: 0

Views: 89

Answers (2)

Oz Ben-David
Oz Ben-David

Reputation: 1657

It seems to me that you should read a little but about Routes and their part in the grand scheme of Rails

You can start here http://guides.rubyonrails.org/routing.html

You can also look at some RailCast in order to fully grasp the meaning of routes It is a little bit tricky , but when you understand it it fully make sense

If you want the "Map" link to go to a specific map you should change the

<%= link_to "Map", :controller => :maps, :action => :map %>

To

<%= link_to "Map", @map %>

Where @map is the variable of that map that you initialized in the controller

If you want the "Map" link to go to the maps index page and display all the maps you should change the

<%= link_to "Map", :controller => :maps, :action => :map %>

To

<%= link_to "Map", maps_path %>

Upvotes: 1

Pierre-Louis Gottfrois
Pierre-Louis Gottfrois

Reputation: 17631

Well this is how REST resource and URL are defined. Read here

When doing the following in your routes.rb file:

resource :maps

You are telling rails to generate default routes for you for the following actions:

  • index => GET /maps
  • show => GET /maps/1
  • new => GET /maps/new
  • create => POST /maps
  • edit => GET /maps/1
  • update => PUT /maps/1
  • destroy => DELETE /maps/1

Upvotes: 0

Related Questions