开发者

What is the difference between a restful route method for getting an index vs. creating a new object?

According to rake routes, there's the same path for getting an index of objects as there is for creating a new object:

cars GET    /cars(.:format)    {:controller=>"plugs", :what=>"car", :action=>"index"}
     POST   /cars(.:format)    {:controller=>"plugs", :what=>"car", :action=>"create"}

Obviously, the HTTP verb is what distinguishes between them. I want the "create" version of the cars_path method, not the "index" version. My question is what route method do you invoke to choose the one you want? I'm telling cucumber what path to generate with this:

when /the car plug preview page for "(.+)"/
  cars_path(:action => :create, :method => :post)

...but it always chooses the "index" action, not "create". I've tried lots of combinations for the hash argument following cars_path and nothing changes it from choosing "index" instead of "create".

I'll get an error like this:

cars_开发者_运维问答url failed to generate from {:controller=>"plugs", :method=>:post, 
:what=>"car", :action=>"create"}, expected: {:controller=>"plugs", :what=>"car",
:action=>"index"}, diff: {:method=>:post, :action=>"index"} 
(ActionController::RoutingError) 

This seems like a very simple question but I've had no luck googling for it, so could use some advice. Thanks.


Since the URL is the same for both actions, you can use cars_path (without arguments) in both cases. You just simply have to make sure that the form's method-parameter is set to :post. You can not set the method via the URL, you need to set it for the form (and you can't reach the create action by using a link, you need to use a form).


The difference is that one is accessed when a POST is performed, the other is accessed when a GET is performed. Typing a URL into the browser or (typically) clicking a link is the equivalent of a GET action. POST actions are typically performed by form submissions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜