2010-09-24 159 views
0

好吧,所以我正在做一个调度。帮助与轨道协会

所以我有两个表到目前为止, 显示“标题:字符串”和“描述:文本”,我也有 ShowTime; “show_id:integer”,“day:string”和“show_time:time”。

我做的has_many和belongs_to的,我真的不知道从哪里从这里走,

我希望用户能够创建一个新的节目时添加的时间。我会怎么做?我一直在寻找一些轨协会文档,好像我将作出类似的东西,

@showtime = @shows.showtimes.create(:show_id => show.id, :day => DAY, :show_time => TIME) 

通知我把刚才的日期和时间,因为我也真的不知道我怎么会取这个数据。

回答

2

这真的取决于你的界面。但为了简单起见,我们假设您提供了两个选择日期和时间的选择框,并且必须逐个添加ShowTime

而且假设你有其他资源:

map.resources :shows do |show| 
    show.resources :show_times 
end 

形式:(给已经创建了一个@show对象)

<% 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 %> 

你应该提供生成day & show_time阵列的最佳途径。它们的结构如下:

[["Text", "value"], ["Text", "value"]]

这将产生类似:

<option value="value">Text</option>

形式提交后,在您创建行动:

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 
+0

嘿彼得,谢谢你的帮助。我想我需要阅读更多的教程,因为我不明白你说的一半。我没有休息资源,到目前为止,我只做了show&showtime scaffolds,迁移,然后添加了“has_many”和“belongs_to”。 – Rickmasta 2010-09-25 02:01:30

+0

如果你打开'routes.rb',你会看到两个资源'map.resources:shows'和'map.resources:showtimes'。脚手架也使用平静的资源。但恐怕它不会为您生成嵌套资源。是这样吗? – PeterWong 2010-09-25 15:15:38