为什么我需要更努力地使我的 Rails 应用程序适合 RESTful 架构? [英] Why do I need to work harder to make my Rails application fit into a RESTful architecture?

查看:20
本文介绍了为什么我需要更努力地使我的 Rails 应用程序适合 RESTful 架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started a Rails project recently and decided to use RESTful controllers. I created controllers for my key entities (such as Country) and added index, new, edit, create, show, update and delete. I added my map.resources :country to my routes file and life was good.

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

After development progressed a little, I started to encounter problems. I sometimes needed extra actions in my controller. First there was the search action that returned the options for my fancy autocompleting search box. Then came the need to display the countries in two different ways in different places in the application (the data displayed was different too, so it wasn't just two views) - I added the index_full action. Then I wanted to show a country by name in the URL, not by id so I added the show_by_name action.

当您需要超出标准indexneweditcreateshowupdatedelete 在 Rails 的 RESTful 控制器中?我是否需要在 routes.rb 文件中添加(和维护)手动路由(这很痛苦),它们是否在不同的控制器中,我是否变得不稳定,或者我是否缺少一些基本的东西?

What do you do when you need actions beyond the standard index, new, edit, create, show, update, delete in a RESTful controller in Rails? Do I need to add (and maintain) manual routes in the routes.rb file (which is a pain), do they go in a different controller, do I become unRESTful or am I missing something fundamental?

我想我是在问,我是否需要更努力地工作并将操作添加到我的 routes.rb 文件中以获得 RESTful 的特权?如果我没有使用 map.resources 来添加 REST 好东西,标准的 :controller/:action, :controller/:action/:id 路由会处理很多一切都是自动的.

I guess I am asking, do I need to work harder and add actions into my routes.rb file for the privilege of being RESTful? If I wasn't using map.resources to add the REST goodies, the standard :controller/:action, :controller/:action/:id routes would handle pretty much everything automatically.

推荐答案

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

If I go beyond the standard CRUD actions with my models, I normally just add the methods as required. Searching is something I add to many controllers, but not every one, so I add it and maintain the routes normally:

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

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

Moving these actions to an entirely separate controller might keep some of your controllers RESTful, but I find that keeping them in context is far more useful.

这篇关于为什么我需要更努力地使我的 Rails 应用程序适合 RESTful 架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆