Use the model as a resource or not?

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

User has_many Vote
Post has_many Vote

      

In my unit tests, I was defining a method named @ post.vote_up that creates a vote for a post, but then I started wondering if such an interface could provide a calm methodology.

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

Within that controller, wouldn't it be a bad practice to name something specific like @ post.vote_up ?

Should I just create a member action in the PostController named vote_up and forget to use Vote as a resource?

Thank!

+2


source to share


1 answer


In this situation, you always need to ask yourself:

This action makes sense outside of the context of this application.

If so, then it belongs to the model. Otherwise, it belongs to the controller. Mail voting is a concept that exists outside of the context of your application, and therefore the logic for it must remain in the model.



I think posting to / topic / 1 / votes to create a vote sounds pretty reasonable, and most people will do it.

An alternative is to allow the PUT request to / topic / 1 / vote / username (or some variations of that). This is arguably more decessive than a message and should conform to the HTTP specification for PUT. You will of course need to check the username logged in the username in the url.

+3


source







All Articles