views:

45

answers:

1

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.

+2  A: 

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
PeterWong
Rickmasta
If you open your `routes.rb`, you should see two resources `map.resources :shows` and `map.resources :showtimes`. Scaffolds uses restful resources also. But I'm afraid it didn't generate nested resources for you. Is it the case?
PeterWong