【发布时间】:2015-07-15 01:39:10
【问题描述】:
我有名为 #load 的自定义控制器操作和名为 schedules_controller 的 #save。我也有船模型,其中船has_many: schedules 和时间表belongs_to :boat。
这里是routes.rb;
resources :boats, except: :destroy do
post '/schedules/load' => 'schedules#load'
post '/schedules/save' => 'schedules#save'
end
当我使用这条路线时,rake 给出了;
boat_schedules_load POST /boats/:boat_id/schedules/load(.:format) schedules#load
boat_schedules_save POST /boats/:boat_id/schedules/save(.:format) schedules#save
然后在我的Schedules_controller;
class SchedulesController < ApplicationController
before_filter :load_parent
def save
h = params[:event]
h.each do |key, value|
@boat.schedules.create date: value["date"], notes: value["notes"], price: value["price"], promo: value["promo"], status: value["status"]
end
end
def load
end
def load_parent
@boat = Boat.find(params[:boat_id])
end
end
我有一个模板,我在其中显示日程(日历)并有 Jquery,可能 Jquery 路径错误但我不知道如何在路径中使用 id;
overwiev.html.erb;
......
<div id="backend"></div> <!--This is for the calendar-->
<script>
$(document).ready(function() {
$('#backend').DOPBackendBookingCalendarPRO({
'DataURL': '/schedules/load', #THIS IS WRONG I SHOULD SMT LIKE PUT /boat_id/schedules/load
'SaveURL': '/:boat_id/schedules/save'
});
});
</script>
所以当我运行代码时,它给出了一个错误;
Routing Error
No route matches [POST] "/schedules/load"
我也有load.json.erb 文件,我想从数据库中加载数据,
<% schedule = @boat.schedules.all %>
<% if schedule.blank? %>
{"2010-01-08": {"available":"1",
"bind":0,
"info":"",
"notes":"",
"price":"20",
"promo":"",
"status":""
}}
<% else %>
{"<%= schedule.last.date %>": {"available":"<%= schedule.last.available %>",
"bind":"<%= schedule.last.bind %>",
"info":"<%= schedule.last.info %>",
"notes":"<%= schedule.last.notes %>",
"price":"<%= schedule.last.price %>",
"promo":"<%= schedule.last.promo %>",
"status":"<%= schedule.last.status %>"
}}
<% end %>
因为我在控制器上获得了船 ID,所以我认为 <% schedule = @boat.schedules.all %> 代码应该可以工作。但是,我尝试将路由(未嵌套)取出为;
resources :boats
post '/schedules/load' => 'schedules#load'
post '/schedules/save' => 'schedules#save'
然后报错;
ActiveRecord::RecordNotFound in SchedulesController#load
Couldn't find Boat with 'id'=
【问题讨论】:
标签: jquery ruby-on-rails routes nested