我有三個模型「事件」,「團隊」和「用戶」與各種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