I have several models:
Schedule
A user
instance can be of type :student
or :employer
.
schedule
belongs_to
a project
and project
has_one
schedule
project
belongs_to
a student
and a student
has_many
projects
.
A relationship belongs_to Student and relationship also belongs_to Employer. Employer and Student both has_many relationships.
I am writing the controller to create a schedule and I can't figure out how to tell the controller which project the schedule belongs to. Here is what I have so far
def create
if current_user.type == 'Employer'
redirect_to employer_profile_path(current_user.profile_name)
else
@schedule = Schedule.find(params[:id])
if @schedule.save(schedule_params)
flash[:notice] = "Successfully created schedule."
redirect_to profile_path(current_user.profile_name) #change to project path later
else
render :action => 'new', :notice => 'Invalid Schedule'
end
end
end
private
def schedule_params
params.require(:schedule).permit(tasks_attributes: [:title, :content, :_destroy])
end
I am pretty sure I am defining @schedule incorrectly. The routes for schedule are:
resources :schedules
How do I tell the controller which Project the schedule belongs to?
Assuming you posted the create action for the SchedulesController
there is no need for:
@schedule = Schedule.find(params[:id])
this will probably return nil, because there is no schedule (after all that is what you want to create
here).
@schedule = Schedule.new(schedule_params)
if you want to assign a project to the schedule, you need to submit this information so you can access it in the params
hash. Something like:
@schedule.project = Project.find(params[:project_id])