简体   繁体   English

创建时与Rails相关的模型ID

[英]Rails associated model id on create

I have 2 models, a sport model and a team model. 我有2个模型,一个运动模型和一个团队模型。 The team model belongs_to :sport and the sport model has_many :teams. 团队模型属于:sport,运动模型has_many:teams。

Sport model: 运动款:

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

end

Team Model: 团队模式:

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

When a new team is created it must always associate with a sport. 创建新团队时,它必须始终与一项运动相关联。 So for example if Hockey has an ID of 1, the team that is created under hockey must contain the sport ID. 因此,例如,如果曲棍球的ID为1,则在曲棍球下创建的团队必须包含运动ID。 Below is the current schema: 以下是当前架构:

  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

This is the teams controller: 这是团队负责人:

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

I tried to do the following in the routes: 我尝试在路线中执行以下操作:

resources :sports do
  resources :teams
end

But get an error when trying to create a team from the the following URL: /sports/1/teams/new 但是,尝试从以下URL创建团队时遇到错误:/ sports / 1 / teams / new

The error is: undefined method `teams_path' for #<#:0x007fafb4b9b0c0> 错误是:#<#:0x007fafb4b9b0c0>的未定义方法'teams_path'

app/views/teams/_form.html.erb where line #1 raised: app / views / teams / _form.html.erb,其中第一行出现在:

For your route setup: 对于您的路线设置:

resources :sports do
  resources :teams
end

You will need to use new_sport_team_path which will map to sports/:sport_id/teams/:id/new . 您将需要使用new_sport_team_path ,它将映射到sports/:sport_id/teams/:id/new

And in your app/view/teams/_form.html.erb , since your route is sports/:sport_id/teams , your form_for declaration should be: 并且在您的app/view/teams/_form.html.erb ,由于您的路线是sports/:sport_id/teams ,因此form_for声明应为:

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

In this case sport_teams_path will route to /sports/:sport_id/teams with post method which will execute the create action in your TeamsController . 在这种情况下, sport_teams_path将使用post方法路由到/sports/:sport_id/teams ,该方法将在TeamsController执行create动作。

The form_for declaration above can also be written as: 上面的form_for声明也可以写成:

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

In this case you'd need to define @sport and @team in your controller as follows: 在这种情况下,您需要在控制器中定义@sport@team ,如下所示:

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

For a list of routes defined in your application, you could run rake routes from within your application directory in the terminal. 对于您的应用程序中定义的路由列表,您可以在终端的应用程序目录中运行rake routes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM