I have a User
, Meeting
, and Club
Model.
A User
has_many :user_clubs, has_many :clubs, :through => :user_clubs.
A Club
has_many :user_clubs, has_many :users, :through => :user_clubs, and has_many :meetings
A Meeting
belongs_to :club, and has_many :users
The trouble I'm facing is creating a new meeting through the nested route:
http://localhost:3000/clubs/1/meetings/new
.
When I attempt to create the meeting I'm getting the validation error "Club must exist." I understand that this can be overridden because the belongs_to association is requiring Club. However, I should not need to override it - as EVERY meeting should belong to a club. I just don't know how to correctly solve the error.
My routes are:
resources :users
resources :clubs do
resources :meetings, only: [:show, :index, :new, :create, :edit, :update]
end
resources :meetings, only: [:index, :show, :new, :create, :edit, :update]
My Meetings#new action looks like:
def new
@club = Club.find(params[:club_id])
@meeting = Meeting.new
end
and Meetings#create:
def create
binding.pry
@meeting = Meeting.new(meeting_params)
if @meeting.save
binding.pry
redirect_to club_path, notice: "Meeting was succesfully created."
else
render :new
end
end
My Meetings new
template is using form_for(@meeting) do |f|
. How do I pass the club
that the validation requires to Meetings#create? The flow is:
User show page clicks club > Club show page clicks button to create new meeting > Meetings#new page types in name, description, time and clicks create > Meetings#index page
I'm unable to get past the Meetings#new page.
Solved. The answer was in my form_for
. I needed to change it to:
<%= form_for [@club, @meeting] do |f| %>