1

Routes File:

resources :tournaments do
    resources :game, :only => [:new, :index, :create, :update, :destroy]
end

Rake Routes shows:

 new_tournament_game GET    /tournaments/:tournament_id/game/new(.:format)          game#new

I call:

<td><%= link_to 'Add Game',  new_tournament_game_path(tournament) %></td>

Game Model:

Takes me to the view game view with the URL: http:// local host:3000/tournaments/2/game/new

And view:

<h1>New Game in <%= @tournament.name %> Tournament</h1>

<fieldset>
   <%= form_for [:tournament, @game], :url => tournament_game_index_path do |f| %>
    <table>
      <td>
       .... More fields .....
      </td>
  <div class="form-actions">
    <%= f.submit "Create %>
  </div>
<% end %>

When create is clicked it yields the error:

undefined method `game_url' for #<GamesController:0xb6131e40>



Questions:
Should I be using nested routes or hidden fields?
Do I need a separate tournament_game controller / view to handle the tournament game calls?
How do I get it to look for the correct create route when clicking the Create button?
When I want to have a relationship between two tables, do I only need the nested resource and the has_many / belongs_to calls or do I still need a foreign key column such as Tournament?

Sorry for all of the questions in one thread. Any help you can offer would be greatly appreciated!

Thank you.

Edit:

The error references line 39 which would be the line for the create controller.

    class GamesController < ApplicationController
  # GET /game
  # GET /game.json
  def index
    @game = Game.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @game }
    end
  end

  # GET /game/1
  # GET /game/1.json
  def show
    @game = Game.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @game }
    end
  end

  # GET /game/new
  # GET /game/new.json
  def new
    @game = Game.new
    @tournament = Tournament.find(params[:tournament_id])

  end


  # GET /game/1/edit
  def edit
    @game = Game.find(params[:id])
  end
  # POST /game
  # POST /game.json
  def create
    @tournament = Tournament.find(params[:tournament_id])
    @game = @tournament.game.build(params[:game])

    respond_to do |format|
        if params[:commit] != 'Cancel'
          if @game.save
            format.html { redirect_to @game, notice: 'Game was successfully created.' }
            format.json { render json: @game, status: :created, location: @game }
            format.json { render json: @game }            
          else
            format.html { render action: "new" }
            format.json { render json: @game.errors, status: :unprocessable_entity }
          end
        else
          format.html { redirect_to @game, alert: 'Game was not updated.' }
        end
    end
  end

  # PUT /game/1
 # PUT /game/1.json
  def update
    @game = Game.find(params[:id])


    respond_to do |format|
      if params[:commit] != 'Cancel'
        if @game.update_attributes(params[:game])
          format.html { redirect_to @game, notice: 'Game was successfully updated.' }
          format.json { render json: @game }      
        else
          format.html { render action: "edit" }
          format.json { render json: @game.errors, status: :unprocessable_entity }
        end
      else
          format.html { redirect_to @game, alert: 'Game was not updated.' }
      end
    end
  end

  # DELETE /game/1
  # DELETE /game/1.json
  def destroy
    @game = Game.find(params[:id])
    @game.destroy

    respond_to do |format|
      format.html { redirect_to game_url }
      format.json { head :no_content }
    end
  end
end
3
  • Could you post the error line and the corresponding line from your controller? Commented Feb 8, 2013 at 19:18
  • Could you post the game controller?? Commented Feb 8, 2013 at 19:25
  • Posted above. Thank you! Commented Feb 8, 2013 at 21:42

1 Answer 1

2

Try this:

<%= form_for @game, :url => tournament_games_path(@tournament) do |f| %>

This will call the create method of games controller

Game Controller:

def create
  @tournament = Tournament.find(params[:tournament_id])
  @game = @tournament.games.build(params[:game])
  if @game.save
    flash[:success] = "Game created successfully"
    redirect_to tournaments_path
  else
    render new_tournament_game_path
  end
end

Routes:

resources :tournaments do
   resources :games, :only => [:new, :index, :create, :update, :destroy]
end
Sign up to request clarification or add additional context in comments.

3 Comments

This gives me: No route matches {:action=>"update", :controller=>"games", :tournament_id=>#<Tournament id: 1,
edited the answer.. Why did you use singular in routes..(resources games)?? Try the edited answer..
tournament_game_path(@tournament) will call update method so change it to tournament_games_path(@tournament)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.