Rails - map.resources 的冗余 RESTFUL 操作? (新建、创建)

发布于 2024-08-04 12:46:12 字数 495 浏览 1 评论 0原文

我想知道为什么当您使用 map.resources 在 Rails 中创建静态路线时,它会生成新建、创建、编辑、更新的操作? 仅声明 createupdate 的一个操作是否有问题 并做这样的事情?

def create
  unless post?
     @user = User.new
  else
     redirect_to :action => 'index' if user.create(params[:user])
  end
end

所以我们可以有类似的东西,

:GET  users/create # to show the form (same as action - new)
:POST users/create # to create a new user

因为 Restful 是基于动词的,这不是最好的使用方法吗?

感谢您的关注

I was wondering why when you create restful routes in rails with map.resources it generates actions for new, create, edit, update?
Is there anything wrong in declaring just one action for create and update
and do something like this?

def create
  unless post?
     @user = User.new
  else
     redirect_to :action => 'index' if user.create(params[:user])
  end
end

so we could have something like

:GET  users/create # to show the form (same as action - new)
:POST users/create # to create a new user

since Restful is based on verbs, wouldn't this be the best approach to use?

Thank you for your attention

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-08-11 12:46:12

我认为这里有两个相关但不同的问题:暴露的 URL 和它们路由到的控制器方法。由于其中任何一个都可以独立更改,因此我将分别解决它们。另外,请注意,我将稍微宽松且严格地谈论在 Rails 上下文中实现的 REST。

就外部 URL 而言,我认为这有助于区分构成系统 API 的 URL(:GET users/1:PUT users/1等)以及为人们使用网络浏览器提供方便的 URL(users/newusers/5/edit 等)。 API 的作用就是获取资源或以某种方式与资源进行交互 - 这些是另一台计算机在与您的系统交互时将使用的 URL。这些 URL 通常只是您想要与之交互的资源的地址,然后您使用 HTTP 方法和参数来指示您想要执行的操作(GET = 显示此资源,PUT = 更改此资源等) )。便利 URL 用于显示表单,以便人们更轻松地使用 API。您可以通过使用curl手动输入您想要更改的所有参数并向users/1发送POST来编辑用户,但作为一个人,如果您可以只使用表单,那就容易多了。

要查看上面的示例, :GET users/create 可能有意义(并且与默认的 :GET users/new 非常相似),但是 < code>:POST users/create 大致会翻译为“创建一个新的用户/创建”,这不太有意义。

就控制器方法而言,“new”和“create”正在执行根本不同的任务,希望从前面的段落中可以清楚地看出这一点。其中一个是显示表单,另一个是创建新资源。当然,您可以重载相同的方法来执行此操作,但如果没有令人信服的理由这样做,创建两个小的独立方法来处理两个小的独立任务可能是更自然的方法。

I think there are two related but distinct issues here: the URLs exposed and the controller methods they are routed to. Since either of these could be changed independently, I'll address them separately. Also, please note that I will be speaking a bit loosely, and strictly about REST as implemented in the context of Rails.

In terms of the external URLs, I think it helps to distinguish between the URLs that make up the API of the system (:GET users/1, :PUT users/1, etc) and the URLs that are just there as a convenience to humans using a web browser (users/new, users/5/edit, etc). The API is all about fetching resources or interacting with them in some way - these are the URLs that another computer is going to use when interacting with your system. These URLs are typically just the address of the resource you want to interact with, and then you use the HTTP method and the parameters to indicate what it is you want to do (GET = show me this resource, PUT = change this resource, etc). The convenience URLs are there to display a form to make it easier for a human to use the API. You could edit a user by using curl to manually type out all the parameters you wanted to change and make a POST to users/1, but as a human it's a lot easier if you can just use a form.

To look at your examples above, then, :GET users/create might make sense (and is pretty similar to :GET users/new which is the default), but :POST users/create would roughly translate to "make a new one of users/create", which doesn't quite make sense.

As far as the controller methods go, "new" and "create" are performing fundamentally different tasks, as should hopefully be clear from the previous paragraphs. One of them is displaying a form, and the other is creating a new resource. You could overload the same method to do this, of course, but without a compelling reason to do so, creating two small independent methods to handle two small independent tasks is probably a more natural approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文