Forum: Ruby on Rails Restfully Noob

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2007-01-08 03:26
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
63bae00378a3ccca653aac7660784773?d=identicon&s=25 isaac (Guest)
on 2007-01-08 05:34
(Received via mailing list)
Hi David,

The last route is correct, but what is the chuck method doing ?

maybe you're looking for :collection => {:chuck => :get}
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2007-01-08 15:46
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/anythingthatdoesntm...

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!
63bae00378a3ccca653aac7660784773?d=identicon&s=25 isaac (Guest)
on 2007-01-09 11:34
(Received via mailing list)
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:

1) 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


2) 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
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2007-01-09 15:44
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
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2007-01-09 20:16
(Received via mailing list)
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.
63bae00378a3ccca653aac7660784773?d=identicon&s=25 isaac (Guest)
on 2007-01-19 16:31
(Received via mailing list)
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.
This topic is locked and can not be replied to.