我有三个模型“事件”,“团队”和“用户”与各种ActiveRecord关联,并且无法创建新的Event对象并将其与team_id关联。也许我错过了与关系的一些东西。它们的定义如下:Ruby On Rails - 通过关联的新模型对象
class User < ActiveRecord::Base
has_many :teams
has_many :events, through: :teams
end
class Team < ActiveRecord::Base
has_many :events
has_many :users
end
class Event < ActiveRecord::Base
belongs_to :team
has_many :users, through: :teams
end
class EventsController < ApplicationController
def new
@event = Event.new
end
def create
@event = current_team.events.build(event_params)
if @event.save
flash[:success] = "Event created!"
redirect_to @event
else
render 'new'
end
end
class TeamsController < ApplicationController
def new
@team = Team.new
end
def create
@team = current_user.teams.build(team_params)
if @team.save
flash[:success] = "Team created!"
redirect_to @team
else
render 'new'
end
end
错误在事件触发控制器,当我提交了创建新活动形式,如current_team.events无法识别。我对RoR比较陌生,所以任何帮助都将不胜感激!
CURRENT_USER是从来没有在球队控制器定义
隐藏字段,这是因为CURRENT_USER在轨道隐含定义? – ncarroll
它通常会带来这种功能的设计宝石,但它会是current_user,而不是current_team! – born4new