vendredi 3 avril 2015

Trying to get nested objects to be given parent ids

Alright, So I have days, tracks, and events. When I create a track, it is given the proper day_id along with its own track_id . When I try to create an event, I want it to be given the appropriate day_id and/or track_id.


So when I pull up a day it should only show the tracks and events that as associated with the day. An event should exist in a track, which exists in a day.


So, this is basically a calendar that shows a daily schedule of different tracks, and the events within the tracks.


Also, my events table in my db has a track_id and a day_id.


Here is my routes.rb



resources :days do
resources :tracks do
resources :events
end
end


My new/create methods from my events_controller.rb



def new
@day = Day.find(params[:day_id])
@track = Track.find(params[:track_id])
@new_event = Event.new
end

def create
@day = Day.find(params[:day_id])
@track = Track.find(params[:track_id])
@events = @track.events
@new_event = Event.new(params.require(:event).permit(:name,:start_time, :end_time))


if @new_event.save
flash[:notice] = "Saved"
redirect_to @day
else
flash[:error] = "Error"
render :new
end
end


And here is where I am trying to create a new event in my events.new.html.erb



<%= form_for [@day, @track, @new_event] do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control', placeholder: "Enter the event" %>

<%= f.label :start_time %>
<%= f.text_field :start_time, class: 'form-control', placeholder: "Enter a start time"%>

<%= f.label :end_time %>
<%=f.text_field :end_time, class: 'form-control', placeholder: "Enter a start time"%>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>


Thanks for the help, I'm pretty sure I attached everything that was needed!


Aucun commentaire:

Enregistrer un commentaire