Rails 1.2 routes question


#1

Hi,

I am getting to grips with Raisl 1.2 and am using the new restfull
features. I have my model article and used scaffold_resource article to
geive me the restfull default crud methods. So in the routes.rb i have

map.resources :articles

in the articles controller i want to add a custom method lets say list
and have a list view to show all the articles in the model.

def list
@articles = Article.find(:all)
end

What is the best way to then access the list method in the articles
controller. If i try http://localhost:3004/articles/list i get an
error. Do i need to set up a named route or can i add something to the
map.resources :articles to let it know to accept requests for the list
action?

Is using map.connect :controller/:action/:id, :controller =‘xxxx’,
:action =’’ not good practice in this version?

Any advice appreciated

JB


#2
  1. Why not just use the already declared index method?

  2. If you really have to have an additional list method, just add

:collection => {:list => :get} # Presuming you’re gonna use GET to “get”
the
page/resource

to your mapping for that resource.

But I’d seriously question the need for both an index and a list method.
That’s where people are going to land normally based on the common
assumptions that most routing uses. http://domain.com/controller ==
http://domain.com/controller/index. So you’re either going to risk
reduplication [index == list] or redirection. Either way sounds like
unnecessary work. Yuck.

I can see cases where you might want to use this methodology of a
separate
list method but not for regular cases. And certainly not just because
the
old scaffolding used that method. Ick. Scaffolding.

Excuse my snottiness. I have a cold. :wink:

RSL


#3

Russell N. wrote:

  1. Why not just use the already declared index method?

  2. If you really have to have an additional list method, just add

:collection => {:list => :get} # Presuming you’re gonna use GET to “get”
the
page/resource

to your mapping for that resource.

But I’d seriously question the need for both an index and a list method.
That’s where people are going to land normally based on the common
assumptions that most routing uses. http://domain.com/controller ==
http://domain.com/controller/index. So you’re either going to risk
reduplication [index == list] or redirection. Either way sounds like
unnecessary work. Yuck.

I can see cases where you might want to use this methodology of a
separate
list method but not for regular cases. And certainly not just because
the
old scaffolding used that method. Ick. Scaffolding.

Excuse my snottiness. I have a cold. :wink:

RSL

Thanks for your reply. I was using list as an example but basically i
want to know how to add a another action to a controller that has the
default CRUD actions from using scaffold_resource . What do i put in
the routes.rb file as i already have:

map.resources :articles

So if i had a action called ‘list’ then what is the best way to let
routes.rb know to except requests of http://localhost:3004/articles/list
and what do i put in a view to link th this action. Its an ordinary GET
request so i notice similar to edit_article_path have a
list_article_path.

Is this even possible or do i create a named route for each extra action
that is on the controller?


#4

So if i had a action called ‘list’ then what is the best way to let
routes.rb know to except requests of http://localhost:3004/articles/list

Using REST, you can write the url:
http://localhost:3004articles and that will invoke the index method,
which would be your list method.

To create a link from a view, you would use:
articles_path

And in your routes.rb file you’d declare each RESTful controller:
map.resources :articles as you are already, and add a new declaration
for each controller.

I found the tutorial as Peep Code very useful.

If you still want to use the /articles/list url, you would set the
route something like:
map.connect ‘:controller/:action/’


#5

toby privett wrote:

So if i had a action called ‘list’ then what is the best way to let
routes.rb know to except requests of http://localhost:3004/articles/list

Using REST, you can write the url:
http://localhost:3004articles and that will invoke the index method,
which would be your list method.

To create a link from a view, you would use:
articles_path

And in your routes.rb file you’d declare each RESTful controller:
map.resources :articles as you are already, and add a new declaration
for each controller.

I found the tutorial as Peep Code very useful.

If you still want to use the /articles/list url, you would set the
route something like:
map.connect ‘:controller/:action/’

I think using list as an example is confusing things. I know about the
default routes you get with map.resources :articles and the paths to
access these routes like edit_article_path and that index should be used
for the list the items.

Say i wanted an action called search on this articles controller so in
the view i have a file search.rhtml What is the best way for me to
serve up this file, can i add it to the existing map.resources :articles
by adding something that was mentioned in the first reply :collection =>
{:search => :get} , do i create a named route and place that before
map.resources: articles or do i have a map.connect
':controller/:action/ like in rails 1.6

Sorry for the confusion, i know about the defaults urls and how they
work its adding more actions to a controller that already has the
default CRUD actions that im looking for advice on.


#6

using :collection => { :search => :get } in your map.resource
declaration in the routes.rb file is the correct way to add custom
actions to your routes.

Like this:

map.resources :zip_codes, :collection => { :search => :get }

Controller:

GET /zip_codes;search

GET /zip_codes.xml;search

def search
end

The URL would be something like:
http://domain.com/articles;search

Just like using some of the standard resource routes like for edit:
http://domain.com/articles;edit

On Feb 19, 10:36 am, John B. removed_email_address@domain.invalid


#7

By the way there are also ways to add routes for singular and new
resources like this:

Singular route:
map.resources :articles, :member => { :embargo => :put, :release
=> :put }

New Route:
map.resources :articles, :new => { :shortform => :post }


#8

Robert W. wrote:

By the way there are also ways to add routes for singular and new
resources like this:

Singular route:
map.resources :articles, :member => { :embargo => :put, :release
=> :put }

New Route:
map.resources :articles, :new => { :shortform => :post }

And what is the best way to access this new route in the view:

<%= link_to “search”, :controller=>“articles”, :action=>“search” %>

or can you do something like search_articles_path ???


#9

Sorry, I forgot that part.

Do so like this:
<%= link_to search_articles_path %>

On Feb 19, 11:14 am, John B. removed_email_address@domain.invalid