I’ve been playing with REST today and can’t quite figure out a routing
issue.
I have a controller named Accounts which has 3 actions:
1. new
2. create
3. chuck
My routes file has the following:
map.resources :accounts, :member => { :chuck => :get }
map.connect ':controller/:action/:id'
These are the urls I would expect to work, but they both produce errors:
http://localhost:3000/accounts/;chuck
Produces: Routing Error: no route found
to match "/accounts/;chuck" with {:method=>:get}
or
http://localhost:3000/accounts/chuck
Produces: Unknown action: no action responded to show
This actually works, but
http://localhost:3000/accounts/1;chuck
but doesn't really contain a valid id, can put anything where the 1
is.
Just wondering what I am doing wrong?
Thanks,
Dave
Hi David,
The last route is correct, but what is the chuck method doing ?
maybe you’re looking for :collection => {:chuck => :get}
isaac wrote:
Hi David,
The last route is correct, but what is the chuck method doing ?
maybe you’re looking for :collection => {:chuck => :get}
Hi Isaac,
Chuck really doesn’t do anything, I was just messing with trying to add
a non-standard REST action to a controller.
The thing that’s weird is:
http://localhost:3000/accounts/1;chuck
http://localhost:3000/accounts/beetlejuice;chuck
http://localhost:3000/accounts/anythingthatdoesntmatter;chuck
all work.
I want the chuck action to function like a nonREST action. The action
requires no id, yet since it’s a get, it requires an id.
In other words, I’d like the “chuck” action to function like the “index”
action, since “index” requires no id.
Hope that makes sense.
Thanks!
isaac wrote:
Hi Isaac,
Just trying to see what’s possible with map.resources. I got the new
agile 1.2 book - but it really doesn’t explain the difference between a
collection and a member very well.
Thanks again.
Dave
Hi Dave,
Just trying to see what’s possible with map.resources. I got
the new agile 1.2 book - but it really doesn’t explain the difference
between a collection and a member very well.
In a Rails way, it would be Collection has_many :members,
Member belongs_to :collection
-- Jean-François.
–
À la renverse.
In this context a member would mean an individual instance of the model
Account.
A collection would be interested in more than one member of the model
Account.
So the index route/action/view is a collection because it is accessing
a collection of accounts (possibly all accounts)
The show route/action/view however is a member because it is only
interested in one member of the model Account. (hence :id being part
of the route - this is how the individual member is identified)
I hope this is helpful and not more confusing
disclaimer: I am quite new to ruby/rails/REST so please don’t take my
word as gospel.
Its not really that weird is it ?
In the second case its passing beetlejuice as the id, your controller
doesn’t know its an invalid id until you try and find the record -
@account = Account.find(params[:id])
I think you have 2 choices here:
- You can keep everything RESTful and add chuck in like this:
map.resources :accounts, :collection => {:chuck => :get}
which will give you the route /accounts;chuck
- you can include a custom route before your map.resources like this:
map.connect ‘accounts/chuck’, :controller => ‘accounts’, :action =>
‘chuck’
which give you the route /accounts/chuck
Your choice…#2 is not the restful way AFAIK