views:

28

answers:

1

I have model/controller called notification.

I want to make a new url so that I can access it by:

/notifications/my_new_url?id=4 and have that page go to view my_new_url.html.erb

however, it always keeps going to show method:

This is in my routes.rb

  map.resources :notifications

  map.connect 'notifications/get',
              :controller => 'notifications',
              :action     => 'show'            

  map.connect 'notifications/my_new_url',
              :controller => 'notifications',
              :action     => 'my_new_url'            

Please assist me...I've been stuck on this for a while

A: 

You need to map the URL to a controller and action combination. But your action can render the view. You also need need to map the specialized URLs before URLs with wildcards. So do this:

# config/routes.rb
    ...
    map.connect 'notifications/my_new_url',
                :controller => 'notifications',
                :action => 'my_new_url'

    map.resources :notifications
    ...


# app/controllers/notifications_controller.rb
  ...
  def my_new_url
    respond_to do |format|
      format.html { render :my_new_url }
    end
  end
  ...

# app/views/notifications/my_new_url.html.erb
  ...
Justice
still going to `show` method :(
samwick
for the mean time I've added if/else in show method to render different pages
samwick
Put `map.resources :notifications` after all special routes within 'notifications/***'.
Justice
That route should also be defined before the routes `map.resources :notifications` because the first route the url matches will be the one it follows, and that route will also match the show (which is why it keeps going to the show). Also, there should be some syntax to specify a collection, which is a much smarter route. Unfortunately Rails, is going through so many changes right now, and the router in particular has gone through so many changes in the lifetime of Rails, that I am not sure where to point to.
Joshua Cheek
@samwick - did you reload your server/application after adding the route?
Beerlington