Can a route require POST or GET? / REST problem with routing

Hi,

A store front a customer wants to GET /product/5/show. But for the
application this is really GET /cart_item/new/5 or even better
/cart_item/new?cart_item[product_id]=5

When the customer clicks add_to_cart they POST /product/5/show so that
the url doesn’t confuse the user when any validation errors occur. But
for the applicaiton this should be POST
/cart_item/create?cart_item[quantity]=23

Can I use routing to check the request method something like the
following?

map.connect ‘product/:id/show’, :controller=>‘cart_item’,
:action=>‘new’, :id=>/\d+/, :method=>‘get’
map.connect ‘product/:id/show’, :controller=>‘cart_item’,
:action=>‘create’, :id=>/\d+/, :method=>‘post’

Thanks,
Peter

You can verify post for certain methods in your controller by adding:

class YourController < ApplicationController

verify :method => :post, :only => %w( action1 action2 etc… )

Regards,
Ian

On 7/7/06, Peter M. [email protected] wrote:

Can I use routing to check the request method something like the following?

map.connect ‘product/:id/show’, :controller=>‘cart_item’,
:action=>‘new’, :id=>/\d+/, :method=>‘get’
map.connect ‘product/:id/show’, :controller=>‘cart_item’,
:action=>‘create’, :id=>/\d+/, :method=>‘post’

Try :conditions => { :method => ‘post’ } at the end of your route

Tom

On 7/7/06, Tom W. [email protected] wrote:

On 7/7/06, Peter M. [email protected] wrote:

Can I use routing to check the request method something like the following?

map.connect ‘product/:id/show’, :controller=>‘cart_item’,
:action=>‘new’, :id=>/\d+/, :method=>‘get’
map.connect ‘product/:id/show’, :controller=>‘cart_item’,
:action=>‘create’, :id=>/\d+/, :method=>‘post’

Try :conditions => { :method => ‘post’ } at the end of your route

Tom,

Thanks for the reply. This doesn’t seem to work. I am using Rails
1.1.4. Do I need a plugin for this to work? I see the error at the
bottom of this email.

Look at the poor routing docs

http://api.rubyonrails.com/files/vendor/rails/actionpack/lib/action_controller/routing_rb.html

Nothing:S

Peter

=> Booting WEBrick…
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/routing.rb:49:in
test_condition': Valid criteria are strings, regular expressions, true, or nil (ArgumentError) from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/code_generation.rb:228:incheck_conditions’
from
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/code_generation.rb:226:in
check_conditions' from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/routing.rb:362:inwrite_generation’
from
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/routing.rb:557:in
generation_code_for' from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/code_generation.rb:19:inindent’
from
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/routing.rb:553:in
generation_code_for' from /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/routing.rb:492:ineach_with_index’
from
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.3/lib/action_controller/routing.rb:550:in
generation_code_for' ... 19 levels... from /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.4/lib/commands/server.rb:30 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require’
from script/server:3

On 7/7/06, Peter M. [email protected] wrote:

Try :conditions => { :method => ‘post’ } at the end of your route

Tom,

Thanks for the reply. This doesn’t seem to work. I am using Rails
1.1.4. Do I need a plugin for this to work? I see the error at the
bottom of this email.

I’m using edge rails and defining my routes using the simply_restful
plugin. It’s possible the http method conditions are only part of the
latest routing code. I’m afraid I’m not sure.

Tom

Hello Peter,

2006/7/7, Peter M. [email protected]:

Tom,

Thanks for the reply. This doesn’t seem to work. I am using Rails
1.1.4. Do I need a plugin for this to work? I see the error at the
bottom of this email.

you can write :require => { :method => :post } in your route, but
this feature is only available in Edge, if I’m not wrong.

РJean-Fran̤ois.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs