views:

53

answers:

1

So I have a User, Post, and Vote models.

User has_many Vote
Post has_many Vote

In my unit tests I was defining a method called @post.vote_up which creates a vote for a post, but then I started thinking whether or not such an interface would allow for restful methodology.

If I were to call /topic/1/votes with a POST, the VoteController's create action would be called.

Within that controller wouldn't it be bad practive calling something as specific as @post.vote_up ?

Should I simply create a member action in the PostController called vote_up and forget using Vote as a resource?

Thanks!

+3  A: 

What you always need to ask yourself in this situation is this:

Does this action make sense outside the context of this application.

If it does then it belongs in the model. Otherwise it belongs in the controller. Voting up a post is a concept that does exist outside the context of your application, and so the logic for it should remain in the model.

I think that posting to /topic/1/votes to create a vote sounds pretty reasonable, and is what most people would do.

An alternative is to allow for a PUT request to /topic/1/votes/username (or some variation of that). This is arguably more deescriptive than the post and should follow the HTTP specification for PUT. You would of course need to verify the logged in username against the username in the URL.

Jack Ryan