简体   繁体   English

Rails Restful actions Index Put

[英]Rails Restful actions Index Put

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". 我经常遇到我想要一次更新许多记录的情况 - 就像GMail设置许多消息“读取”或“未读”一样。

Rails encourages this with the 'update' method on an ActiveRecord class - Comment.update(keys, values) Rails通过ActiveRecord类的'update'方法鼓励这一点 - Comment.update(keys,values)

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

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| %>
  ...

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. 如果你这样做:: collection => {:update_many =>:put},rails会向索引操作提交一个帖子(CommentsController #index),我希望它转到'update_many'动作。 Instead, you can do a :collection => { :update_many => :post }. 相反,你可以做:collection => {:update_many =>:post}。 This will at least go to the correct action in the controller. 这至少会在控制器中执行正确的操作。

And, instead of <% form for @comments ... you have to do the following: 并且,而不是<%form for @comments ...您必须执行以下操作:

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

It will work OK this way 它会以这种方式工作

Still not perfect - feels a little like we're not doing it the 'Rails way'. 仍然不完美 - 感觉有点像我们没有做'Rails方式'。 It also seems like :post, and :delete would also make sense on a collection controller. 它看起来也像:post,and: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? 关于如何安静地进行收集级别的任何其他想法:post,:put,:delete?

I've run into a few situations like you describe. 我遇到过你所描述的一些情况。 The first couple of times I've implemented form almost identical to the one you suggest. 我实施的前几次几乎与你建议的相同。

About the third time I hit this problem I realized that every item I'm updating has a common belongs_to relationship with something else. 大约第三次我遇到这个问题时,我意识到我正在更新的每个项目都有一个与其他东西有共同的belongs_to关系。 Usually a user. 通常是用户。 That's exactly the epiphany you need to make sense of this RESTfully. 这正是你需要理解这个RESTful的顿悟。 It will also help you clean clean up the form/controller. 它还可以帮助您清理表单/控制器。

Don't think of it as updating a bunch of messages, think of it as updating one user. 不要将其视为更新一堆消息,将其视为更新一个用户。

Here's some example code I've used in the past to highlight the difference. 这是我过去用来突出差异的一些示例代码。 Assuming that we we want bulk operations on messages that belong to the current_user... 假设我们要对属于current_user的消息进行批量操作...

As of rails 2.3 we can add 从rails 2.3我们可以添加

 accepts_nested_attributes_for :messages

to the user model. 到用户模型。 Ensure messages_attributes is part of attr_accessible, or is not attr_protected. 确保messages_attributes是attr_accessible的一部分,或者不是attr_protected。

Then create the route: 然后创建路线:

 map.resources :users, :member => {:bulk_message_update, :method => :put}

Then add the action to the controller. 然后将操作添加到控制器。 With AJAX capabilities ;) 具有AJAX功能;)

 def bulk_message_update
   @user = User.find(params[:id])
   @user.update_attributes(params[:user])
   if @user.save 
     respond_to do |format|
       format.html {redirect}
       format.js {render :update do |page|
          ...
       } 
     end       
   else
     ....
 end 

Then your form will look like this: 然后你的表单将如下所示:

<% form_for current_user, bulk_message_update_user_url(current_user), 
     :html => {:method => :put} do |f| %>
  <% f.fields_for :messages do |message| %>
    form for each message
  <% end %>
  <%= sumbit_tag %>
<% end %>

I often add collection-based update_multiple and destroy_multiple actions to an otherwise RESTful controller. 我经常将基于集合的update_multiple和destroy_multiple操作添加到其他RESTful控制器。

Check out this Railscast on Updating Through Checkboxes. 查看有关通过复选框更新的Railscast It should give you a good idea how to approach it, come back and add to this question if you run into troubles! 它应该让你知道如何处理它,如果你遇到麻烦,回过头来添加这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM