Routes problem

Hello all,
Here’s a newbie question (4 weeks and counting).
My routes.rb is as below.

map.resources :projects, :departments, :users, :admins, :imports, :notes

#Below is route in question
map.resources :projects, :collection => { :view_all => :get }

map.home ‘’, :controller => ‘projects’, :action => ‘index’
map.connect ‘:controller/:action/:id.:format’
map.connect ‘:controller/:action/:id’

I’m tried to get a url like this:
http://localhost:3000/projects/view_all

I have a view_all in the ProjectsController and I have a view_all
template.

When I type in the desired url, I get this error:

"ActiveRecord::RecordNotFound in ProjectsController#show

Couldn’t find Project with ID=view_all"

Thank you for any help.

JohnM

John M. wrote:

Hello all,
Here’s a newbie question (4 weeks and counting).
My routes.rb is as below.

map.resources :projects, :departments, :users, :admins, :imports, :notes

#Below is route in question
map.resources :projects, :collection => { :view_all => :get }

map.home ‘’, :controller => ‘projects’, :action => ‘index’
map.connect ‘:controller/:action/:id.:format’
map.connect ‘:controller/:action/:id’

I’m tried to get a url like this:
http://localhost:3000/projects/view_all

I have a view_all in the ProjectsController and I have a view_all
template.

When I type in the desired url, I get this error:

"ActiveRecord::RecordNotFound in ProjectsController#show

Couldn’t find Project with ID=view_all"

Thank you for any help.

JohnM

Your view_all == default index

That being said, when you add a controller method like view_all, you’ll
need to tell the routes about it (add view_all as a get method for the
collection)

map.resources :projects, :collection => {:view_all => :get}

On Aug 28, 2009, at 11:34 AM, John M. wrote:

Hello all,
Here’s a newbie question (4 weeks and counting).
My routes.rb is as below.

map
.resources :projects, :departments, :users, :admins, :imports, :notes

#Below is route in question
map.resources :projects, :collection => { :view_all => :get }

Routes have preference based on their order of appearance. The first
time you create routes for :projects, there’s a show route like /
projects/:id and that matches before the second one.

Combine those into:

map.resources :projects, :collection => { :view_all => :get }
map.resources :departments, :users, :admins, :imports, :notes

HOWEVER, the regular route:
/projects
is normally going to show you all the projects anyway.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Since I’ve been learning rails, I thought that routes are the linking of
the “Controller” Object to their “Action” method.
When you set up RESTful routing, you need to add custom or named routes
after the default 7.
To set up custom or named routes, you use “map.connect” “path/to/view”,
:controller => ‘controller’, :aciton => ‘action’

Is this correct or am I lost?

Rob B. wrote:

On Aug 28, 2009, at 11:34 AM, John M. wrote:

Hello all,
Here’s a newbie question (4 weeks and counting).
My routes.rb is as below.

map
.resources :projects, :departments, :users, :admins, :imports, :notes

#Below is route in question
map.resources :projects, :collection => { :view_all => :get }

Routes have preference based on their order of appearance. The first
time you create routes for :projects, there’s a show route like /
projects/:id and that matches before the second one.

Combine those into:

map.resources :projects, :collection => { :view_all => :get }
map.resources :departments, :users, :admins, :imports, :notes

HOWEVER, the regular route:
/projects
is normally going to show you all the projects anyway.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Hi John,
try this approach:
in the Product class add this function-

require ‘uri’
def self.find_custom arg
object = self.new
object.id = URI.escape(arg)
object
end

and in the controller try calling as:
Product.find_custom(params[:id]).view_all

just try it, and let me know.

On Fri, Aug 28, 2009 at 9:04 PM, John M. <
[email protected]> wrote:

map.home ‘’, :controller => ‘projects’, :action => ‘index’


Regards,
Himanshu

ROR will attempt to find routes from the top of the file first and then
move
on to the rest of the file. It would seem that you have listed projects
twice:

map.resources :projects, :departments, :users, :admins, :imports,
:notes

#Below is route in question
map.resources :projects, :collection => { :view_all => :get }

The first one (without the :collection) would be used.

Hope this helps.

On Fri, Aug 28, 2009 at 11:34 AM, John M. <

John M. wrote:

Since I’ve been learning rails, I thought that routes are the linking of
the “Controller” Object to their “Action” method.
When you set up RESTful routing, you need to add custom or named routes
after the default 7.

I tend to think of it as adding them ‘inline’

As Rob suggested:

Combine those into:

map.resources :projects, :collection => { :view_all => :get }

The above line gets you the default routes, and adds a route that works
on the projects collection called ‘view_all’ and is a GET request. Just
what your view_all route will do is up to the controller code for that
method, but we assumed that you’re returning all the projects, which is
what ‘index’ does.
Hence my earlier point that your ‘show_all’ is functionally the same as
the default ‘index’, and unnecessary…

There is also the ability to add routes that deal with a specific
project. You’d specify this type of route using the :member
specification.

map.resources :projects, :member => {:as_pdf => :get}

This route is intended to operate against a single project, and is a GET
request.

My app has a project model as well, and it’s map.resources line looks
like this:
map.resources :projects, :member => {:as_pdf => :get, :clone => :get,
:trace => :get}

This yields the index, show, new, create, edit, update, and destroy
standard routes, as well as:
an ‘as_pdf’ route (to generate a pdf of the project info),
a ‘clone’ route (makes a deep clone of an existing project), and
a ‘trace’ route which generates a traceability view of the project)

Run a rake routes >routes.lst command from the root of your app, then
look insude routes.lst to see what’s being generated for you by restful
routing. It’s very informative.

Another tip is to run “rake routes” to see all the available routes.

On Aug 28, 2:43 pm, Himanshu P. [email protected] wrote:

and in the controller try calling as:
Product.find_custom(params[:id]).view_all

just try it, and let me know.

This code is so messed up it’s almost not even wrong. What exactly is
it supposed to do?

–Matt J.

On Aug 28, 2009, at 2:02 PM, John M. wrote:

Since I’ve been learning rails, I thought that routes are the
linking of the “Controller” Object to their “Action” method.
When you set up RESTful routing, you need to add custom or named
routes after the default 7.
To set up custom or named routes, you use “map.connect” “path/to/
view”,
:controller => ‘controller’, :aciton => ‘action’

Is this correct or am I lost?

Not lost, just not yet there. As you’ve no doubt seen from other
responses, you had :projects in BOTH places. Personally, I never
define more than one resource route at a time.

map.resources :projects, :collection => { :view_all => :get }
map.resources :departments
map.resources :users
map.resources :admins
map.resources :imports
map.resources :notes

If you need to add other routes to them, you’ll need to pull separate
them anyway.

-Rob