# Map.resources with both :get and :post on an action

**URL:** <https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425>\
**Category:** Rails\
**Created:** [June 3, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425 "2009-06-03T16:48:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [June 3, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425/1 "2009-06-03T16:48:50Z")

</div>

hi,  
using map.resources i want an action to have both :get and :post http  
verbs.

map.resources :tickets,  
:path\_prefix =\> '/workflows,  
:collection =\> { :pending =\> [:get, :post],  
:download =\> :get }

here the pending action responds to post, i think the route was  
overwritten.  
I can use :any as the HTTP verb but i want the route to be more  
explicit.  
I am using rails 2.1.1.

when i try to access  
‘/workflows/tickets/pending’ i get a Routing Error.  
No route matches “/workflows/tickets/pending” with {:method=\>:get}

Please Help

Thanks,  
Deepak

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [June 4, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425/2 "2009-06-04T15:47:55Z")

</div>

does { :pending =\> :any} work? Being able to specify multiple discrete  
methods was an addition in 2.3 I believe.

Glenn

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [June 4, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425/3 "2009-06-04T17:29:53Z")

</div>

deepak wrote:

> map.resources :tickets,  
> :path\_prefix =\> '/workflows,  
> :collection =\> { :pending =\> [:get, :post],  
> :download =\> :get }

I’m not sure what you “pending” action does, but you should keep this in  
mind:

## Excerpt from HTTP 1.1 specification:

7.1. Safe and Idempotent Methods

7.1.1. Safe Methods

Implementors should be aware that the software represents the user in  
their interactions over the Internet, and should be careful to allow  
the user to be aware of any actions they might take which may have an  
unexpected significance to themselves or others.

In particular, the convention has been established that the GET and  
HEAD methods SHOULD NOT have the significance of taking an action  
other than retrieval. These methods ought to be considered “safe”.  
This allows user agents to represent other methods, such as POST, PUT  
and DELETE, in a special way, so that the user is made aware of the  
fact that a possibly unsafe action is being requested.

## Naturally, it is not possible to ensure that the server does not generate side-effects as a result of performing a GET request; in fact, some dynamic resources consider that a feature. The important distinction here is that the user did not request the side-effects, so therefore cannot be held accountable for them.

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [June 4, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425/4 "2009-06-04T20:49:46Z")

</div>

the ‘pending action’ shows a list of tickets, we apply some filters on  
them like from and to date and other filters.  
Then i have to process this ticket, wherein we contact an external  
api, some db transactions and generate a csv report.

i would like to POST to the same url to process the tickets as  
selected on the GET.  
The get request is Idempotent and can be done as many times as needed,  
but the POST request is destructive. I am not very good at REST  
principles but this is RESTful i believe.

In the older rails scaffolding, if i remember correct, the new and  
save action was the same but we GET or POSTed on that action. In  
RESTful rails we have a new action with the GET http verb and a save  
action with POST, is this more idiomatic rails to have one action with  
either GET or POST but not both? Even if it is not the rails way, how  
would i express it in rails routes DSL.

Basically, it is a workflow with multiple steps. Tentatively what i  
have thought about is that every step is an action. We GET the action  
and POST to the same action after which we are redirected to another  
action. A better solution or an example of a good piece of code would  
be nice.

Thanks

On Jun 4, 8:29 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [June 4, 2009, 8:18pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425/5 "2009-06-04T20:18:32Z")

</div>

{ :pending =\> :any} worked but then i had to write.

if request.get?

# …

elsif request.post?

# …

else  
raise(ActionController::RoutingError,  
“Route #{request.path} is valid for get or post, not for #  
{request.method}”)  
end

I wanted to specify this in the route.rb and make it more  
declarative.

How do is specify this using the routes DSL?

---

<div class="post-metadata">

**Author:** ![deepak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7bcc69/32.png) [@deepak](https://www.ruby-forum.com/u/deepak)\
**Post date:** [June 4, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/map-resources-with-both-get-and-post-on-an-action/169425/6 "2009-06-04T21:17:23Z")

</div>

You have to specify different actions in your controller for get and  
post. Then in routes.rb you can declare the same url to get routed to  
either one of these actions depending on the http verb.

2009/6/4, deepak [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> principles but this is RESTful i believe.  
> and POST to the same action after which we are redirected to another
> 
> > > map.resources :tickets,
> > 
> > This allows user agents to represent other methods, such as POST, PUT  
> > Posted viahttp://www.ruby-forum.com/.

–  
Von meinen Mobilgerät aus gesendet
