路线档案:
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://本地主持人:3000 /锦标赛/ 2 /游戏/新
并查看:
<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>
的的问题:
我应该使用嵌套路线还是隐藏字段?
我是否需要一个单独的tournament_game控制器/视图来处理锦标赛游戏电话?
如何在单击“创建”按钮时查找正确的创建路径?
当我想在两个表之间建立关系时,我是否只需要嵌套资源和has_many / belongs_to调用,还是需要一个外键列,比如锦标赛?
对于一个帖子中的所有问题,我们深表歉意。我们非常感谢您提供的任何帮助!
谢谢。
编辑:
错误引用第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
答案 0 :(得分: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