Jordi
Jordi

Reputation: 1324

how to RESTful-ize a complex controller

I have a controller with actions:

class GameController < ApplicationController
before_filter :set_titles

def prestart
end

def wait
end

def play (game)
end


def button
end

def finish
end


def set_titles
end

set_titles will get the page title (and other general properties for all actions) All other actions check for something and render a view.

How to RESTful-ize this. I can only think of creating a Wait_player controller a play_game controller etc , all of them will only have a show action. But this looks clumbsy and more difficult to follow that all these related actions in the same controller.

Upvotes: 3

Views: 282

Answers (3)

Ethan
Ethan

Reputation: 60099

First off, I would think about whether a RESTful design is the best choice for this controller. Some domains just don't fit all that well with REST and if you try to jam them into it you multiply work and confusion. I don't know your app well enough to answer that, but it's something to think about.

As mentioned, REST deals with nouns. Looking at what you've posted, the main noun I see is game. So to make it RESTful, what you're calling "prestart" could be new and your "play" could be create. "Finish" could be destroy. "Wait" could just remain "wait". Not every action in a RESTful controller has to be one of the standard seven. I don't think there's any RESTful equivalent of "wait".

You also have button. It's hard to say without knowing more about your app, but perhaps that should get its own controller. Generally with RESTful Rails app each distinct entity gets its own controller with one or more of the seven standard actions.

"How to RESTful-ize this. I can only think of creating a Wait_player controller a play_game controller etc."

That approach is more like thinking about creating a controller for each action you want to do. Intead, try thinking about a controller for each thing you want to act on. For example, instead of WaitPlayer controller, make it a Player controller with show, new, create, and so on (the standard RESTful actions), and then additionally perhaps a wait action.

Upvotes: 5

nasmorn
nasmorn

Reputation: 2150

This doesn't merely look clumsy and more difficult, it sure is. REST is not for everything

But you could bind pre_start to create, finish to delete, play to update and wait to show, then you are still left with putting the button action somewhere (I suggest inventing a better name)

PS: set_titles should be private

Upvotes: 2

Tad Donaghe
Tad Donaghe

Reputation: 6588

You need to think about the Nouns in your system and not your verbs. REST deals with nouns - creating, reading, updating and deleting THINGS. What are the things in your system?

Looks like you have Titles and a Game, etc.

RESTful APIs require a different way of approaching problems.

This link talks about RESTful WCF services, but the stuff about thinking in a RESTful manner should apply in your domain as well.

Upvotes: 1

Related Questions