“从副本中新建”的 REST 路径

发布于 2024-12-01 23:40:38 字数 609 浏览 1 评论 0原文

对于某些模型,我希望提供允许用户基于现有记录的副本创建具有默认属性的新记录的功能。

我想知道正确的休息路线是什么。

我最初的想法是它可能是 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 技术交流群。

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

发布评论

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

评论(4

寒冷纷飞旳雪 2024-12-08 23:40:38

我的问题不是“我如何在 Rails 中做到这一点?”,而是“它是 RESTful 吗?”

不,不是。就此而言,GET /photos/new 也不是。 Rails 似乎无可救药地陷入了过去,当时它被认为是高级程序,用于在 URI 上进行 GET 返回 HTML 表单,然后将 x-www-form-urlencoded 数据 POST 返回到该表单URI。该 POST 的不透明性迫使他们发明新的动词作为 URI,例如 /photos/new,而您可以使用 PUT 代替,或者至少使用相同媒体类型的 POST。

以 REST 方式复制 HTTP 资源的最简单方法是:

GET /photos/{id}/ -> [representation of a photo resource]
...make modifications to that representation as desired...
POST /photos/ <- [modified representation]

如果您要为浏览器实现此操作,您应该能够非常轻松地通过 Ajax 执行这些操作,使用可能位于 /photos/manager 的 HTML 页面.html/ 来驱动与用户的交互。

my question is not "how do I do this in rails?", it's "is it RESTful?"

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:

GET /photos/{id}/ -> [representation of a photo resource]
...make modifications to that representation as desired...
POST /photos/ <- [modified representation]

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.

ζ澈沫 2024-12-08 23:40:38

您可以尝试使用嵌套资源。我不太确定您的应用程序的结构,但一般来说,使用嵌套照片会看起来像这样:

routes.rb

resources :photos do
  resources :photos
end

photos_controller.rb

before_filter :find_parent_photo, :only => [:new, :create]

def create
  @photo = Photo.new params[:photo]
  if @parent_photo.present?
    # fill some @photo fields from @parent_photo
  end
  @photo.save

  respond_with @photo
end

def find_parent_photo
  @parent_photo = Photo.find(params[:photo_id]) if params[:photo_id].present?
end

new.html .haml

= form_for [@parent_photo, @photo] do |f|
  -# your form code

以前,当您想要添加照片创建的链接时,您

= link_to "new photo", [:new, :photo]

现在会写类似的内容,如果您想添加基于 foto @photo1 的照片创建的链接

= link_to "new photo based on other one", [:new, @photo1, :photo]

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

resources :photos do
  resources :photos
end

photos_controller.rb

before_filter :find_parent_photo, :only => [:new, :create]

def create
  @photo = Photo.new params[:photo]
  if @parent_photo.present?
    # fill some @photo fields from @parent_photo
  end
  @photo.save

  respond_with @photo
end

def find_parent_photo
  @parent_photo = Photo.find(params[:photo_id]) if params[:photo_id].present?
end

new.html.haml

= form_for [@parent_photo, @photo] do |f|
  -# your form code

previously when you wanted to add a link to photo creation you wrote something like that

= link_to "new photo", [:new, :photo]

now if you want to add a link to photo creation based on foto @photo1

= link_to "new photo based on other one", [:new, @photo1, :photo]
剑心龙吟 2024-12-08 23:40:38

您应该能够像这样匹配路由:

match 'photos/new/:photo_id' => 'photos#new

或者您可以只在url中传递:photo_id参数并在控制器中处理它:

'/photos/new?photo_id=17'

使用辅助方法的示例:< code>new_photo_path(:photo_id => 17)

编辑:我不知道这是否符合 REST

You should be able to match a route like so:

match 'photos/new/:photo_id' => 'photos#new

or you could just pass a :photo_id parameter in the url and handle it in the controller:

'/photos/new?photo_id=17'

Example using helper method: new_photo_path(:photo_id => 17)

Edit: I don't know if this conforms to REST

赏烟花じ飞满天 2024-12-08 23:40:38

可以这样做:

class PhotoCopiesController < ApplicationController
  def new
    @photo = Photo.find(params[:photo_id]).dup
  end

  def create
  end
end

resources :photo_copies, :only => [:new, :create]

它可能有点夸张,但你

= link_to 'Copy', photo_copy_path(:photo_id => @photo.id)

It may be over the top, but you could do something like this:

class PhotoCopiesController < ApplicationController
  def new
    @photo = Photo.find(params[:photo_id]).dup
  end

  def create
  end
end

and

resources :photo_copies, :only => [:new, :create]

and

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