Rails在创建时关联模型ID

时间:2013-12-21 21:10:05

标签: ruby-on-rails

我有2个模特,一个运动模型和一个团队模型。团队模型belongs_to:sport和运动模型has_many:团队。

运动模特:

class Sport < ActiveRecord::Base
    has_many :teams
    has_many :competitions
    has_many :games

end

团队模型:

class Team < ActiveRecord::Base
    belongs_to :sport
    has_many :competition_teams
    has_many :competitions, :through => :competition_teams
    has_many :home_games, :foreign_key => "home_team_id", :class_name => "Game"
    has_many :visiting_games, :foreign_key => "visiting_team_id", :class_name => "Game"
end

创建新团队时,必须始终与体育相关联。因此,例如,如果Hockey的ID为1,则在曲棍球下创建的团队必须包含运动ID。以下是当前架构:

  create_table "sports", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "teams", force: true do |t|
    t.string   "name"
    t.integer  "sport_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

这是球队的控制者:

class TeamsController < ApplicationController
        before_action :set_team, only: [:show, :edit, :update, :destroy]

  # GET /games
  # GET /games.json
  def index
    @teams = Team.all
  end

  # GET /games/1
  # GET /games/1.json
  def show
  end

  # GET /games/new
  def new
    @team = Team.new
  end

  # GET /games/1/edit
  def edit
  end

  # POST /games
  # POST /games.json
  def create
    @team = Team.new(team_params)

    respond_to do |format|
      if @team.save
        format.html { redirect_to @team, notice: 'team was successfully created.' }
        format.json { render action: 'show', status: :created, location: @team }
      else
        format.html { render action: 'new' }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /games/1
  # PATCH/PUT /games/1.json
  def update
    respond_to do |format|
      if @team.update(team_params)
        format.html { redirect_to @team, notice: 'team was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @team.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /games/1
  # DELETE /games/1.json
  def destroy
    @team.destroy
    respond_to do |format|
      format.html { redirect_to sports_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_team
      @team = Team.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def team_params
      params[:team].permit(:name, :sport_id)
    end

end

我尝试在路线中执行以下操作:

resources :sports do
  resources :teams
end

但尝试使用以下网址创建团队时出错:/ sports / 1 / teams / new

错误是:#&lt;#:0x007fafb4b9b0c0&gt;

的未定义方法`teams_path'

app / views / teams / _form.html.erb第1行引出:

1 个答案:

答案 0 :(得分:1)

对于您的路线设置:

resources :sports do
  resources :teams
end

您需要使用new_sport_team_path来映射sports/:sport_id/teams/:id/new

在您的app/view/teams/_form.html.erb中,由于您的路线为sports/:sport_id/teams,因此您的form_for声明应为:

<%= form_for @comment, url: sport_teams_path ... %>
...
<% end %>

在这种情况下,sport_teams_path将使用/sports/:sport_id/teams方法路由到post,该方法将执行create中的TeamsController操作。

上面的form_for声明也可以写成:

<%= form_for([@sport, @team]) ... %>
...
<% end %>

在这种情况下,您需要在控制器中定义@sport@team,如下所示:

# app/controllers/teams_controller.rb
def new
  @sport = Sport.find(params[:sport_id])
  @team = @sport.teams.build
  ...
end

对于应用程序中定义的路由列表,您可以从终端的应用程序目录中运行rake routes

相关问题