"Event"、 "Team"、 "User"の3つのモデルがあり、新しい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は、チームのコントローラで定義されることはありません形式で
隠しフィールドは、これです? – ncarroll
これは通常、この機能をもたらすdevise gemですが、current_teamではなくcurrent_userになります! – born4new