为每个批量更新或许多URL设置一个URL是否更好?

时间:2012-01-23 15:28:27

标签: ruby-on-rails rest

我有一个用户维护页面。此页面包含管理员可以对其选择的用户进行批量更新的用户列表。批量更新包括:激活,停用和更新角色。

如果有一个我发布的URL,例如/users/bulk_update.json,我会在其中传递ID列表和方法类型。在我的bulk_update操作中,我根据方法更新ID。

或者每个方法都应该有一个网址,例如/users/bulk_update_activate/users/bulk_update_deactivate/users/bulk_update_roles

1 个答案:

答案 0 :(得分:0)

快速答案是:这取决于! :)

如果您的更新类型共享许多代码逻辑。

1)使用过滤器:

class FirstController < ApplicationController
  # Other controller code

  before_filter :prepare_update
  after_filter :finalize_update

  def bulk_update_activate
    # Do something here
  end

  def bulk_update_deactivate
    # Do something here
  end
end

2)使用一个动作:

class SecondController < ApplicationController
  # Other controller code

  def bulk_update
    case params[:operation]
      when :activate then
        # Do something here
      when :deactivate then
        # Do something here
    end
  end
end

如果您的更新完全独立,那么您应该编写不同的操作。

在我的项目中,我经常发现自己正在使用第一种方法。

希望它会有用。