这真的取决于你的界面。但为了简单起见,我们假设您提供了两个选择日期和时间的选择框,并且必须逐个添加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
嘿彼得,谢谢你的帮助。我想我需要阅读更多的教程,因为我不明白你说的一半。我没有休息资源,到目前为止,我只做了show&showtime scaffolds,迁移,然后添加了“has_many”和“belongs_to”。 – Rickmasta 2010-09-25 02:01:30
如果你打开'routes.rb',你会看到两个资源'map.resources:shows'和'map.resources:showtimes'。脚手架也使用平静的资源。但恐怕它不会为您生成嵌套资源。是这样吗? – PeterWong 2010-09-25 15:15:38