在Rails中为用户和管理员创建用户控制器的最佳方法

时间:2015-10-29 13:44:58

标签: ruby-on-rails ruby

# # Set this property to true to enable JSON web services. Note that setting # this to false will cause portlets that make JSON web service calls from # working. # json.web.service.enabled=true # # Input a list of comma delimited HTTP methods that are not accessible. For # example, if you want to have a read only API, then set this property to # "DELETE,POST,PUT". # jsonws.web.service.invalid.http.methods= # # Set this property to true to ensure that a JSON web service action can # only be invoked by its expected HTTP method. # # For example, the annotation # com.liferay.portal.kernel.jsonwebservice.JSONWebService may configure the # expected HTTP method for an action to be a GET request. However, the URL # for that GET request may be too long, and so you may need to call that # with a POST request. Setting this property to false loosens this # requirement and allows the POST request to invoke an action that is # supposed to only be called by a GET request. # jsonws.web.service.strict.http.method=false # # The property "jsonws.web.service.paths.excludes" denotes patterns for JSON # web service action paths that are not allowed even if they match one of # the patterns set in "jsonws.web.service.paths.includes". # jsonws.web.service.paths.excludes= # # The property "jsonws.web.service.paths.includes" denotes patterns for JSON # web service action paths that are allowed. Set a blank pattern to allow # any service action path. # jsonws.web.service.paths.includes= User controller

创建User的最佳方式是什么?

在我的情况下,我为Admin创建了一个名称空间,其中包含Admin控制器,然后对于User,我在Users名称空间中创建了另一个Controller。但我感觉我的代码是User DRY,因为用户可以更新他们的详细信息,管理员可以执行完整的CRUD。以下是我做的事情

NOT

所以问题是我如何为Admin和User创建Uses控制器而不重复代码

PS:我不想使用任何active_admin或任何其他类似的admin gem来执行此操作

1 个答案:

答案 0 :(得分:1)

admin是用户的子类吗?如果是这样,您可以执行以下操作:

class UsersController < ApplicationController
  before_action :require_admin, only: [:index, :show, :new, :create, :destroy]
  before_action :require_user, only: [:edit, :update]

  def index
    # ...
  end

  def show
    # Will fetch either User or Admin based on id
    @user = User.find(params[:id])
  end

  # ...

  private

  def require_admin
    unless current_user && current_user.is_a?(Admin)
      redirect_to sign_in_url, alert: "You must be an admin."
    end
  end

  def require_user
    unless current_user
      redirect_to sign_in_url, alert: "You must be signed in."
    end
  end
end

否则,如果它们是两个不同的类和表,我认为使用单独的控制器来管理它们是合适的。

根据评论进行更新

如果某些操作具有相同的功能,而其他操作具有不同的功能,则可以执行某些操作。

class UsersController < ApplicationController
  def edit
    # same for both
  end

  def update
    # same for both
  end
end

然后将它们子类化

class User::UsersController < ::UsersController
  def show
    # different
  end
end

class Admin::UsersController < ::UsersController
  def show
    # different
  end

  def create
    # admin-only
  end
end

我认为这有点令人费解和困惑,但你可以看看它是否适合你。

就个人而言,我会创建一个全局UsersController,管理员和用户用于更新他们自己的用户。然后将管理员单独Admin::UsersController用于CRUD 任何用户。