1

路线文件:

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

Rake Routes 显示:

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

我打电话:

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

游戏模型:

使用以下 URL 将我带到查看游戏视图:http://local host:3000/tournaments/2/game/new

并查看:

<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 %>

单击创建时会产生错误:

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



问题:
我应该使用嵌套路由还是隐藏字段?
我是否需要单独的锦标赛游戏控制器/视图来处理锦标赛游戏调用?
单击“创建”按钮时,如何让它寻找正确的创建路线?
当我想在两个表之间建立关系时,我只需要嵌套资源和 has_many/belongs_to 调用还是仍然需要诸如 Tournament 之类的外键列?

很抱歉在一个线程中的所有问题。您能提供的任何帮助将不胜感激!

谢谢你。

编辑:

错误引用了第 39 行,这将是创建控制器的行。

    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
4

1 回答 1

2

试试这个:

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

这将调用控制器的create方法games

游戏控制器:

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

路线:

resources :tournaments do
   resources :games, :only => [:new, :index, :create, :update, :destroy]
end
于 2013-02-08T19:16:15.900 回答