Ruby on rails 如何从一个模型传递参数';s将页面显示到另一个型号';谁来创建控制器?

Ruby on rails 如何从一个模型传递参数';s将页面显示到另一个型号';谁来创建控制器?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有用户、游戏和玩家模型,在我的游戏显示页面中,我试图创建一个与当前用户和游戏关联的玩家 所以我在blah.com/game/1(显示id为1的游戏页面)上,想按一个按钮创建一个玩家 在我的游戏的显示页面中: # I have @game here which is a reference to the game for this page # can I use it here to fill in @player.game ? <%= form_for(@player) d

我有用户、游戏和玩家模型,在我的游戏显示页面中,我试图创建一个与当前用户和游戏关联的玩家

所以我在
blah.com/game/1
(显示id为1的游戏页面)上,想按一个按钮创建一个玩家

在我的游戏的显示页面中:

  # I have @game here which is a reference to the game for this page
  # can I use it here to fill in @player.game ?
  <%= form_for(@player) do |f| %>
      <%= f.submit "Create player for this game (join this game)" %>
  <% end %>
我相信我需要手动填写游戏的参数,但我不确定如何获得我拥有的游戏的引用。我想我需要在创建控制器中填写参数:

  @terra_player = current_user.players.build(:game => ???) # approximation of how it works
class PlayersController < ApplicationController
  def create
    @game = Game.find(params[:game_id])
    @player = @game.players.build(:user => current_user)
    if @player.save
      redirect_to @game
    else
      render "new"
    end
  end
end

或者在“显示”页面中设置它。但我不确定在这两种情况下是如何发生的。

你们的模型在这里有点古怪;我想说你需要清理你的语义。我只是想尝试一下,但我的猜测是,玩家与游戏的联系比用户更紧密,这是一个更好的选择。您的模型可能应该如下所示:

class User < ActiveRecord::Base
  has_many :players
  has_many :games, :through => :players
end

class Game < ActiveRecord::Base
  has_many :players
  validate :max_players_in_game #left as exercise to reader
end

class Player < ActiveRecord::Base
  belongs_to :user
  belongs_to :game
end
所以你的URL看起来像这样:
POST/games/1/players
。在PlayerController中:

  @terra_player = current_user.players.build(:game => ???) # approximation of how it works
class PlayersController < ApplicationController
  def create
    @game = Game.find(params[:game_id])
    @player = @game.players.build(:user => current_user)
    if @player.save
      redirect_to @game
    else
      render "new"
    end
  end
end
class PlayersController当前用户)
如果@player.save
将_重定向到@game
其他的
呈现“新”
结束
结束
结束

您的型号在这里有点古怪;我想说你需要清理你的语义。我只是想尝试一下,但我的猜测是,玩家与游戏的联系比用户更紧密,这是一个更好的选择。您的模型可能应该如下所示:

class User < ActiveRecord::Base
  has_many :players
  has_many :games, :through => :players
end

class Game < ActiveRecord::Base
  has_many :players
  validate :max_players_in_game #left as exercise to reader
end

class Player < ActiveRecord::Base
  belongs_to :user
  belongs_to :game
end
所以你的URL看起来像这样:
POST/games/1/players
。在PlayerController中:

  @terra_player = current_user.players.build(:game => ???) # approximation of how it works
class PlayersController < ApplicationController
  def create
    @game = Game.find(params[:game_id])
    @player = @game.players.build(:user => current_user)
    if @player.save
      redirect_to @game
    else
      render "new"
    end
  end
end
class PlayersController当前用户)
如果@player.save
将_重定向到@game
其他的
呈现“新”
结束
结束
结束
一种令人毛骨悚然的方式是

MyController.new.create_method parameters
我建议不要这样做。:-)

一种令人毛骨悚然的方式是

MyController.new.create_method parameters

我建议不要这样做。:-)

谢谢你的帮助,这几乎正是我的类的设置方式,但是以游戏为中心而不是以用户为中心是一个非常好的观点。但我首先遇到的问题是,我不知道如何获得:game_id在
def create
中有效。看起来不是这样的,因为您可能缺少设置的嵌套路由部分。使用如上所述的嵌套路由集,您的params散列将有一个:game_id集(来自/games/:game_id/players)。如果您是rails新手,您可能没有意识到这一点,但在控制台上运行
rake routes
将吐出您设置的所有路由,每一个路由都会给你一些细节,关于将调用什么控制器/方法,以及你希望为每一个路由设置什么参数。谢谢你的帮助,这几乎就是我的类的设置方式,但是游戏焦点而不是用户焦点是一个很好的观点。但我首先遇到的问题是,我不知道如何获得:game_id在
def create
中有效。看起来不是这样的,因为您可能缺少设置的嵌套路由部分。使用如上所述的嵌套路由集,您的params散列将有一个:game_id集(来自/games/:game_id/players)。如果您是rails新手,您可能没有意识到这一点,但在控制台上运行
rake routes
将吐出您设置的所有路由,每个路由都会为您提供一些详细信息,说明将调用什么控制器/方法,以及您希望为每个路由设置什么参数。