Rails宁静动作索引放置 [英] Rails Restful actions Index Put

查看:68
本文介绍了Rails宁静动作索引放置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常遇到想要一次更新许多记录的情况-就像GMail设置许多邮件为已读"或未读"一样.

I have frequently run into the situation where I want to update many records at once - like GMail does with setting many messages "read" or "unread".

Rails在ActiveRecord类上通过"update"方法鼓励这样做-Comment.update(keys,values)

Rails encourages this with the 'update' method on an ActiveRecord class - Comment.update(keys, values)

示例- http://snippets.dzone.com/posts/show/7495

这是一项很棒的功能,但很难映射到一条宁静的路线.从某种意义上说,我希望在集合上看到:put动作.在路线中,我们可能会添加类似

This is great functionality, but hard to map to a restful route. In a sense, I'd like to see a :put action on a collection. In routes, we might add something like

map.resources :comments, :collection => { :update_many => :put }

然后在表单中,您将执行此操作...

And then in the form, you'd do this...

<% form_for @comments do |f| %>
  ...

这在许多级别上都行不通.如果您这样做::collection => {:update_many =>:put},rails将向post操作(CommentsController#index)提交一个帖子,我希望它转到"update_many"操作.相反,您可以执行:collection => {:update_many =>:post}.这至少将执行控制器中的正确操作.

This doesn't work on many levels. If you do this: :collection => { :update_many => :put }, rails will submit a post to the index action (CommentsController#index), I want it to go to the 'update_many' action. Instead, you can do a :collection => { :update_many => :post }. This will at least go to the correct action in the controller.

并且,代替@comments的<%形式...,您必须执行以下操作:

And, instead of <% form for @comments ... you have to do the following:

<% form_for :comments, :url => { :controller => :comments, :action => :update_many } do |f| %>

这样可以正常工作

仍然不完美-感觉有点像我们没有按照路轨方式"进行操作.似乎:post和:delete在集合控制器上也很有意义.

Still not perfect - feels a little like we're not doing it the 'Rails way'. It also seems like :post, and :delete would also make sense on a collection controller.

我要在此处发布信息,以查看是否有任何设置方面的内容.关于如何平稳地执行集合级别的任何其他想法:post,:put,:delete?

I'm posting here to see if there's anything I missed on setting this up. Any other thoughts on how to restfully do a collection level :post, :put, :delete?

推荐答案

我经常将基于集合的update_multiple和destroy_multiple操作添加到其他RESTful控制器中.

I often add collection-based update_multiple and destroy_multiple actions to an otherwise RESTful controller.

在通过复选框更新中查看铁路广播.如果您遇到麻烦,它应该给您一个很好的主意,如何解决它,再回来添加这个问题!

Check out this Railscast on Updating Through Checkboxes. It should give you a good idea how to approach it, come back and add to this question if you run into troubles!

这篇关于Rails宁静动作索引放置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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