Jordi
Jordi

Reputation: 1324

Rails, calling actions of the same controller from the view in a restul way?

I am developing an application that is supposed to have games, when one clicks on the game it will go to game.prestart with instructions and other stuff. There I wrote:

 link_to "Play", :controller => "games", :action => "wait"

wait will wait for other players and later redirect to the "play" action

Two questions here:

1.- This is not restful but I dont know how to do it restful, start a game (show?) have 3 phases: prestart, wait and the play itself

2.- That code above will give a "no post action wait" or something like that, if I add :method =>:get I will get a No route matches. I have the method "wait" and the view already created.

Upvotes: 0

Views: 1221

Answers (3)

Omar Qureshi
Omar Qureshi

Reputation: 9093

You can add methods in RESTful routing, besides the default ones. Check out section 3.3, Adding more RESTful actions here.

Actually, what you really want is 3.11

Seems like you need additional member routes defined, that way you can generate paths using:

wait_game_path(@game)

To generate the member route, you will need to modify your map.resources :games line in your routes.rb to be something like:

map.resources :games, :member => {:wait => :get}

This will add a new get action of wait for each instance of @game

Upvotes: 2

On Freund
On Freund

Reputation: 4436

You can add methods in RESTful routing, besides the default ones. Check out section 3.3, Adding more RESTful actions here.

Upvotes: 0

Bogdan Gusiev
Bogdan Gusiev

Reputation: 8305

Here is how you can do that:

  1. Call action play.
  2. Search for sessions marked pending.
  3. If session found then put current session and found session to some wrapping class and notify found session that game accepted(push wrapping class object id).
  4. If session not found then mark session pending for player and redirect user to "Wait page". Schedule the ajax call on "Wait page" that will track the session status if game is accepted from other session.

I hope that was helpful.

Upvotes: 1

Related Questions