【发布时间】:2015-11-16 01:16:13
【问题描述】:
我再次修改了问题以包含控制器文件。
在我们的应用中,我们有三个模型。用户模型、记分牌模型、团队模型。一个用户有_许多记分牌和一个记分牌属于_一个用户。记分板控制器中与两者关联的创建操作的代码是“@scoreboard = current_user.scoreboards.build”。这段代码工作得很好。
现在,第三个模型出现了问题。记分牌模型有_许多队,每个队都属于_一个记分牌。这是一个 has_many,belongs_to 关系。因此,外键在团队表上。下面分别给出记分牌和团队迁移和模型文件。
记分牌模型
class Scoreboard < ActiveRecord::Base
belongs_to :user
has_many :teams, dependent: :destroy
default_scope -> { order(created_at: :desc) }
end
记分牌迁移
class CreateScoreboards < ActiveRecord::Migration
def change
create_table :scoreboards do |t|
t.string :name_of_scoreboard
t.string :name_of_organization
t.string :name_of_activity
t.references :user, index: true
t.timestamps null: false
end
add_foreign_key :scoreboards, :users
add_index :scoreboards, [:user_id, :created_at]
end
end
团队模型
class Team < ActiveRecord::Base
belongs_to :scoreboard
end
团队迁移
class CreateTeams < ActiveRecord::Migration
def change
create_table :teams do |t|
t.string :name
t.integer :win
t.integer :loss
t.integer :tie
t.references :scoreboard, index:true
t.timestamps null: false
end
add_foreign_key :teams, :scoreboards
end
end
我认为我已正确关联模型。因此,我的 Teams 控制器中用于创建操作的代码应正确创建关联。控制器如下:
记分牌控制器:
class ScoreboardsController < ApplicationController
before_action :logged_in_user, only: [:new, :create, :show, :index]
before_action :correct_user, only: [:destroy, :edit, :update]
def new
@scoreboard = Scoreboard.new
end
def create
@scoreboard = current_user.scoreboards.build(scoreboard_params)
if @scoreboard.save
flash[:scoreboard] = "Scoreboard created successfully"
redirect_to scoreboard_path(@scoreboard)
else
render 'new'
end
end
def show
@scoreboard = Scoreboard.find_by_id(params[:id])
end
def index
if params[:search]
@scoreboards = Scoreboard.all.search(params[:search])
else
@scoreboards = current_user.scoreboards
end
end
def edit
@scoreboard = Scoreboard.find_by_id(params[:id])
end
def update
@scoreboard = Scoreboard.find_by_id(params[:id])
if @scoreboard.update_attributes(scoreboard_params)
flash[:success] = "Updated Successfully"
redirect_to scoreboard_path(@scoreboard)
else
render 'edit'
end
end
def destroy
@scoreboard = Scoreboard.find_by_id(params[:id])
@scoreboard.destroy
flash[:success] = "Deleted Successfully."
redirect_to scoreboards_path
end
private
def scoreboard_params
params.require(:scoreboard).permit(:name_of_scoreboard, :name_of_organization,
:name_of_activity, :starts_at, :ends_at, :cities, :states, :country, :picture )
end
def correct_user
@user = Scoreboard.find(params[:id]).user
redirect_to scoreboards_path unless current_user?(@user)
end
end
这是团队控制器:
类 TeamsController 但是,当我提交应用创建操作的表单时,我收到错误“undefined method `teams' for nil:NilClass”。我不确定为什么会发生这种情况,因为我对用户和记分牌模型做了完全相同的事情。 def new
@team = Team.new
end
def create
@scoreboard= current_user.scoreboards.build
@team = @scoreboards.teams.build(team_params)
if @team.save
flash[:success] = "Saved Successfully"
redirect_to scoreboard_path
else
render 'new'
end
end
def index
end
def show
end
private
def team_params
params.require(:team).permit(:name, :win, :loss, :tie)
end
end
【问题讨论】:
-
请包含您的模型文件。在我看来,架构比迁移更有帮助。
-
更清楚,但您能否展示一下包含关联的控制器方法?
标签: ruby-on-rails forms ruby-on-rails-3