开发者

Rails Routes vs URL parameters

I have a double headed question I hope you're able to help me with.

As a bit of context, I have a rails app with a Request model/controller/view setup. It is used for a user to send a request to another user and provides two specific facilities within the update action (trying to stay RESTful) - accept and decline (accept or decline the request).

So my questions are:

  • First, what is the generally accepted approach setting up the url for accessing a specific type of action (in this case accept or decline are both request updates)? Is it better to pass it using a parameter (e.g. /requests/11?response=accept) or to use named routes (e.g. /requests/11/accept)

  • Secondly, if using named routes, how can you determine which named route was responsible for calling the con开发者_如何学JAVAtroller? I think you can use request.url() or something similar to check the actual url (e.g. /requests/11/accept) but that seems to be a brittle approach to me. I've tried to find other possible ways to do this but can't seem to find a decent way.


I prefer /requests/11/accept to /requests/11?response=accept.

Accept and decline are behaviors that will change the state of request resource, so a PUT operation on a single object is appropriate.

You can define a route in your routes config, like below:

resources :requests do
  member {put :accept, :decline}
end

You will get two routes

accept_request maps to {:action=>"accept", :controller=>"requests"}
decline_request maps to {:action=>"decline", :controller=>"requests"}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜