Reputation: 449
Ok guys, so I'm making a scheduler.
So I have two tables so far, Shows with "title:string" and "description:text" and I also have ShowTime; with "show_id:integer", "day:string", and "show_time:time".
I did the has_many, and belongs_to, I honestly do not know where to go from here on,
I want a user to be able to add the times when creating a new show. What would I do? I was looking at some rails associations documentations, seems like I would be making something like,
@showtime = @shows.showtimes.create(:show_id => show.id, :day => DAY, :show_time => TIME)
Notice I put just DAY and TIME, because I also honestly don't know how I'll fetch this data.
Upvotes: 0
Views: 168
Reputation: 16011
It really depends on your interface. But for simplicity, let's assume you provided two select boxes for selecting day and time, and have to add ShowTime
one by one.
And assume you have rest resources:
map.resources :shows do |show|
show.resources :show_times
end
The form: (given a @show object created already)
<% form_for @show_time, :url => show_show_time_path(@show) do |form| %>
<%= form.label :day %>: <%= form.select :day, [["Mon", "mon"], ["Tue", "tue"]], {} %>
<%= form.label :show_time %>: <%= form.select :show_time, [["Slot 1", "09:00"]], {} %>
<% end %>
You should provide your best way to generate the day
& show_time
arrays. They are in the following structure:
[["Text", "value"], ["Text", "value"]]
which will generate something like:
<option value="value">Text</option>
After the form is submitted, in your create action:
def create
@show = Show.find params[:show_id] # this params[:show_id] is from the rest resource's path
@show_time = @show.show_times.build(params[:show_time]) # this params[:show_time] is from the form you submitted
if @show_time.save
flash[:notice] = "Success"
redirect_to show_show_time_path(@show, @show_time)
else
flash[:notice] = "Failed"
render :action => "new"
end
end
Upvotes: 2