29

我最近开始了一个 Rails 项目,并决定使用 RESTful 控制器。我为我的关键实体(例如 Country)创建了控制器并添加了indexneweditcreateshow和. 我将我的添加到我的路线文件中,生活很好。updatedeletemap.resources :country

开发有点进展后,我开始遇到问题。我有时需要在我的控制器中执行额外的操作。首先是search为我喜欢的自动完成搜索框返回选项的操作。然后需要在应用程序的不同位置以两种不同的方式显示国家/地区(显示的数据也不同,因此不仅仅是两个视图) - 我添加了index_full动作。然后我想在 URL 中按名称而不是 id 显示国家/地区,因此我添加了show_by_name操作。

当您需要在 Rails 的 RESTful 控制器中超出标准index, new, edit, create, show, update,的操作时,您会怎么做?delete我是否需要在 routes.rb 文件中添加(和维护)手动路由(这很痛苦),它们是否进入不同的控制器,我是否变得不稳定或我错过了一些基本的东西?

我想我在问,我是否需要更加努力地工作并将操作添加到我的 routes.rb 文件中以获得 RESTful 的特权?如果我不使用map.resources添加 REST 好东西,标准:controller/:action, :controller/:action/:id路由将自动处理几乎所有内容。

4

6 回答 6

13

I would treat search as a special case of index. Both actions return a collection of resources. The request parameters should specify things like page, limit, sort order, and search query.

For example:

/resources/index # normal index
/resources/index?query=foo # search for 'foo'

And in resources_controller:

before_filter :do_some_preprocessing_on_parameters

def index
  @resources = Resource.find_by_param(@preprocessed_params)
end

As for index_full and search_by_name, you might look at splitting your current controller into two. There's a smell about what you've described.

Having said that, you're absolutely right that there's no point in forcing your app to user restful routes when it doesn't deliver anything over /:controller/:action/:id. To make the decision, look how frequently you're using the restful resource route helpers in forms and links. If you're not using them, I wouldn't bother with it.

于 2008-10-23T17:44:42.510 回答
8

如果我的模型超出了标准 CRUD 操作,我通常只根据需要添加方法。搜索是我添加到许多控制器的东西,但不是每个控制器,所以我添加它并正常维护路由:

map.resources :events, :collection => { :search => :get }

将这些操作移动到一个完全独立的控制器可能会使您的一些控制器保持 RESTful,但我发现将它们保持在上下文中更有用。

于 2008-10-23T16:58:21.853 回答
5

REST 未指定您不能拥有其他视图。没有现实世界的应用程序将能够仅使用提供的操作;这就是为什么您可以添加自己的操作。

REST 是关于能够对服务器进行无状态调用。每次返回数据时,您的搜索操作都是无状态的,对吗?您的备用显示操作也是无状态的,只是视图不同。

As to if they should be manual routes or a new controller, that depends on how distinct the activity is. Your alternate view, if it provides a full set of CRUD (create, read, update, delete) operations would do well to be in a new controller. If you only have an alternate view to the data, I would just add an alternate view action.

In other words, it doesn't sound like your application is failing to be RESTful, it is more an issue of realizing that the automatically generated feature set is a starting point, not a conclusion.

于 2008-10-23T17:01:33.640 回答
0

In my opinion they may have gone a bit off the rails here. What happened to DRY?

I'm just getting back into Rails not having done much development with it since beta and I'm still waiting for the light-bulb to come on here. I'm still giving it a chance but if it hasn't happened for me by the end of my current project I'll probably just drop-back to the old standard routes and define the methods as I actually need them for the next one.

于 2009-02-05T16:46:10.437 回答
0

I won't go on to explain more about REST since I think that has been answered in this question, however I will talk a little bit about the default route.

My main problem with the default route is that if you have multiple sites using the same Rails app it can look horrible.

For example there may be controllers that you don't want people to be able to see on one app:

http://example1.somesite.com/example_2/foo/bar/1

compare this to

/:controller/:action/:id

This would go to the controller example_2/foo, action bar and id 1

I consider this to be the main flaw of Rails' default route and this is something that RESTful routes (with subdomain extensions) or only named routes (map.connect 'foo' ... ) can fix.

于 2009-06-07T13:51:05.597 回答
-1

To remain RESTful in your design, you need to rethink what you call a resource.

In your example a show action for a search controller, (search resource) is the direction to remain restful.

In mine, I have a dashboard controller (show) and controllers for single fields of in-place ecditors (show and update)

于 2008-10-25T03:38:55.027 回答