简体   繁体   English

RESTful会员

[英]RESTful membership

I am currentlly trying to design a RESTful MembershipsController. 我目前正在尝试设计一个RESTful MembershipsController。 The controller action update is used only for promoting, banning, approving,... members. 控制器操作更新仅用于提升,禁止,批准...成员。 To invoke the update action the URL must contain a Parameter called type with the appropriate value. 要调用更新操作,URL必须包含带有适当值的名为type的Parameter。 I am not too sure if that is really RESTful design. 我不太确定这是否真的是RESTful设计。 Should I rather introduce sepearate actions for promoting,... members? 我是否应该采取单独的行动来促进...成员?

class MembershipsController < ApplicationController
 def update
    @membership= Membership.find params[:id]
    if Membership.aasm_events.keys.include?(params[:type].to_sym) #[:ban, :promote,...]     
      @membership.send("#{params[:type]}!")
      render :partial => 'update_membership'
    end
  end
end

Neither. 都不是。 The only "actions" a controller should handle are GET, PUT, POST, DELETE (+other http verbs). 控制器应该处理的唯一“动作”是GET,PUT,POST,DELETE(+其他http动词)。 I realize posting this on a question tagged with "rails" is heresy but today I don't care. 我意识到在带有“ rails”标签的问题上发布此内容是异端,但今天我不在乎。

One RESTful way to do this is to create new "processing resources" for each of these operations and POST the member to that resource to invoke the action. 一种执行此操作的RESTful方法是为每个操作创建新的“处理资源”,然后将成员发布到该资源以调用该操作。

When I say create a new resource, you can interpret that to mean, create a new controller. 当我说创建一个新资源时,您可以解释为意味着创建一个新控制器。

To me this is one of the cases when you just shouldn't pull your hair out in efforts to adhere to REST conventions. 对我来说,这就是您不应该为了遵守REST约定而竭尽全力的情况之一。 Your model doesn't seem to fit in with the traditional CRUD concept, and the RESTful principle of differentiating actions via HTTP verbs doesn't seem to belong here too. 您的模型似乎不符合传统的CRUD概念,并且通过HTTP动词区分动作的RESTful原理似乎也不属于这里。

If I were you, I would split that action into separate actions for what you need to do with your memberships (trying to stay as DRY as possible). 如果您是我,我会将该动作分解为针对您需要处理的成员身份的单独动作(尽量保持DRY状态)。 That would make the controller code more readable. 这将使控制器代码更具可读性。 And by creating some routes I would also make the view code cleaner ( promote_membership_path , etc.). 通过创建一些路由,我还将使视图代码更promote_membership_pathpromote_membership_path等)。 But that's just me :), so see what fits most for you. 但这就是我:),所以看看最适合您的是。

EDIT: 编辑:

here is an article which explains my point of view a bit: http://www.therailsway.com/2009/6/22/taking-things-too-far-rest 这是一篇文章,解释了我的观点: http : //www.therailsway.com/2009/6/22/taking-things-too-far-rest

Well, there is more than one way to do things. 好吧,有多种方法可以做事情。 The questions you should be asking yourself, is how many states are there? 您应该问自己的问题是,那里有多少个州? How often do new states get added? 新状态多久添加一次? Etc. 等等。

If there wouldn't be that many states, I would create separate actions + a before filter for the find, but I think this is more of a personal preference. 如果不会有那么多州,我将为查找创建单独的动作+一个前置过滤器,但是我认为这更多是个人喜好。 If you really want to keep it short, you can put each method on one line. 如果您真的想简短一点,可以将每种方法放在一行上。 So: 所以:

class MembershipsController < ApplicationController
  before_filter :find_membership

  def ban;     @membership.ban!;     render :partial => 'update_membership' end
  def promote; @membership.promote!; render :partial => 'update_membership' end

  protected

  def find_membership
    @membership = Membership.find(params[:id)
  end
end

To answer your question whether it is RESTful: yes, your update method is perfectly RESTful, but remember that a PUT should be idempotent. 要回答您是否是RESTful的问题:是的,您的更新方法是完全RESTful的,但是请记住,PUT应该是幂等的。 So if I execute the same method twice, is the result the same? 因此,如果我两次执行相同的方法,结果是否相同? ie what happens if I ban a user and ban him again? 即,如果我禁止一个用户并再次禁止他该怎么办?

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

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