Peter Michaux
2006-Jul-07 07:49 UTC
[Rails] 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
Ian Connor
2006-Jul-07 11:53 UTC
[Rails] Can a route require POST or GET? / REST problem with routing
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 Michaux <petermichaux@gmail.com> wrote:> > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Regards, Ian Connor -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060707/7ef78378/attachment.html
Tom Ward
2006-Jul-07 14:46 UTC
[Rails] Can a route require POST or GET? / REST problem with routing
On 7/7/06, Peter Michaux <petermichaux@gmail.com> 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
Peter Michaux
2006-Jul-07 15:06 UTC
[Rails] Can a route require POST or GET? / REST problem with routing
On 7/7/06, Tom Ward <tom@popdog.net> wrote:> On 7/7/06, Peter Michaux <petermichaux@gmail.com> 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 routeTom, 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:in `check_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:in `write_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:in `indent'' 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:in `each_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:in `require'' 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
Tom Ward
2006-Jul-07 15:24 UTC
[Rails] Can a route require POST or GET? / REST problem with routing
On 7/7/06, Peter Michaux <petermichaux@gmail.com> wrote:> On 7/7/06, Tom Ward <tom@popdog.net> wrote: > > On 7/7/06, Peter Michaux <petermichaux@gmail.com> 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.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
Jean-François
2006-Jul-07 15:38 UTC
[Rails] Can a route require POST or GET? / REST problem with routing
Hello Peter, 2006/7/7, Peter Michaux <petermichaux@gmail.com>:> On 7/7/06, Tom Ward <tom@popdog.net> 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.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. -- ? la renverse.