Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 304
Jordi Avatar asked Nov 18 '25 00:11

Jordi


1 Answers

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.

like image 129
Ethan Avatar answered Nov 19 '25 20:11

Ethan