views:

72

answers:

2

I am currentlly trying to design a 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. I am not too sure if that is really RESTful design. 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
A: 

To me this is one of the cases when you just shouldn't pull your hair out in efforts to adhere to REST conventions. 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.

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

neutrino
Forgive me, but you caught me on a grumpy day. It is no surprise to me that you are a Rails developer. The Rail's definition of REST is a bastardization that bears little resemblance to Roy's definition. REST is quite capable of handling non-CRUD services. It is the Rails framework and its definition of REST that apparently makes it difficult.
Darrel Miller
I'm not sure I understand what you mean by "rails' definition of rest", but in rails restful resources appeared because of the need to simplify handling standard CRUD operations. I didn't mean that REST is incapable of dealing with non-CRUD resources, I meant to say that due to the initial goal of introducing REST to rails, things that are beyond CRUD become harder to stuff into there.
neutrino
I agree with your comment, but that is not how I read your original answer. As far as my comment about the Rails definition of REST, I find that Rails focuses way too much on what the URL looks like and says far too little about the media types being used.
Darrel Miller
+2  A: 

Neither. The only "actions" a controller should handle are GET, PUT, POST, DELETE (+other http verbs). I realize posting this on a question tagged with "rails" is heresy but today I don't care.

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.

When I say create a new resource, you can interpret that to mean, create a new controller.

Darrel Miller
For the reasons discussed in comments to my answer, the tag `rails` is surely needed here, since the way things are arranged in rails has a certain effect on how the author is going to tackle his problem. btw I'm not the one to tell your that sir, but emotions in your words won't help to get your ideas through.
neutrino