我有名为#load
和#save
的自定义控制器操作,名为schedules_controller
。我也有船模,船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 %>
因为我在控制器上获得了船只,我认为<% 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'=
答案 0 :(得分:1)
在控制器中注释过滤器:
#before_filter :load_parent
如果你想使用普通路线
或者您需要在脚本代码中更改路径,如:
$(document).ready(function() {
$('#backend').DOPBackendBookingCalendarPRO({
'DataURL': '/boats/' + boat_id + '/schedules/load', #THIS IS WRONG I SHOULD SMT LIKE PUT /boat_id/schedules/load
'SaveURL': '/boats/' + boat_id + '/schedules/load'
});
});
</script>
需要传递boat_id