gbam
gbam

Reputation: 1484

Ruby on Rails - Passing Parameters between methods

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

Upvotes: 1

Views: 3321

Answers (1)

Rahul Tapali
Rahul Tapali

Reputation: 10147

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

Upvotes: 2

Related Questions