“从副本中新建”的 REST 路径
对于某些模型,我希望提供允许用户基于现有记录的副本创建具有默认属性的新记录的功能。
我想知道正确的休息路线是什么。
我最初的想法是它可能是 new 操作的参数。即借用 Rails Guides 示例,而不仅仅是:
GET : /photos/new
还允许:
GET : / photos/new/:id
...其中 :id
是用作模板的记录的 ID。响应将是一个新的/编辑表单,与普通的旧 new
相同,但值将预先填充现有记录中的数据。参数(或没有参数)可以通过 new 控制器方法轻松处理。
另一种方法似乎是创建一个新的控制器方法,例如 copy
,它也接受现有记录的 id 并使用上面的新表单进行响应。这对我来说似乎有点“不正确”,因为在用户保存新记录(可能对其进行了一些编辑之后)之前,记录实际上并未被复制。
TIA...
更新:我的问题不是“我如何在 Rails 中执行此操作?”,而是“它是 RESTful 吗?”
For certain models, I wish to provide functionality that allows a user to create a new record with default attributes based on copy of an existing record.
I'm wondering what would be the correct restful route for this.
My initial thinking is that it could be a parameter to the new
action. I.e. to borrow from the the Rails Guides examples, instead of just:
GET : /photos/new
Also allow:
GET : /photos/new/:id
...where :id
is the id of the record to use as a template. The response would be a new/edit form, same as with a plain old new
but the values would be pre-filled with data from the existing record. The parameter (or absense of it) could be easily handled by the new
controller method.
The alternative seems to be to create a new controller method, for example copy
which would also accept an id of an existing record and response with the new form as above. This seems a little 'incorrect' to me, as the record is not actually being copied until the user saves the new record (after probably editig it somewhat).
TIA...
UPDATE: my question is not "how do I do this in rails?", it's "is it RESTful?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,不是。就此而言,
GET /photos/new
也不是。 Rails 似乎无可救药地陷入了过去,当时它被认为是高级程序,用于在 URI 上进行 GET 返回 HTML 表单,然后将 x-www-form-urlencoded 数据 POST 返回到该表单URI。该 POST 的不透明性迫使他们发明新的动词作为 URI,例如/photos/new
,而您可以使用 PUT 代替,或者至少使用相同媒体类型的 POST。以 REST 方式复制 HTTP 资源的最简单方法是:
如果您要为浏览器实现此操作,您应该能够非常轻松地通过 Ajax 执行这些操作,使用可能位于
/photos/manager 的 HTML 页面.html/
来驱动与用户的交互。No, it isn't. For that matter, neither is
GET /photos/new
. Rails seems to be hopelessly mired in the past, where it was considered haute programme for a GET on a URI to return an HTML form which would then POST x-www-form-urlencoded data back to that same URI. The opacity of that POST forces them to invent new verbs-as-URI's like/photos/new
, when you could be using PUT instead, or at least POST with the same media type.The simplest way to make a copy of an HTTP resource RESTfully is:
If you're implementing this for browsers, you should be able to perform those actions via Ajax quite easily, using an HTML page sitting perhaps at
/photos/manager.html/
to drive the interaction with the user.您可以尝试使用嵌套资源。我不太确定您的应用程序的结构,但一般来说,使用嵌套照片会看起来像这样:
routes.rb
photos_controller.rb
new.html .haml
以前,当您想要添加照片创建的链接时,您
现在会写类似的内容,如果您想添加基于 foto
@photo1
的照片创建的链接You can try to use nested resources. I'm not exactly sure about structure of you application, but in general using nested photos will look somehow like this:
routes.rb
photos_controller.rb
new.html.haml
previously when you wanted to add a link to photo creation you wrote something like that
now if you want to add a link to photo creation based on foto
@photo1
您应该能够像这样
匹配
路由:或者您可以只在url中传递
:photo_id
参数并在控制器中处理它:使用辅助方法的示例:< code>new_photo_path(:photo_id => 17)
编辑:我不知道这是否符合 REST
You should be able to
match
a route like so:or you could just pass a
:photo_id
parameter in the url and handle it in the controller:Example using helper method:
new_photo_path(:photo_id => 17)
Edit: I don't know if this conforms to REST
可以这样做:
和
它可能有点夸张,但你
It may be over the top, but you could do something like this:
and
and