jquery 中的路线与自定义操作不匹配
Route does not match in jquery with custom action
我有自定义控制器操作 #load
和 #save
名为 schedules_controller
。我还有 Boat Model,其中 Boat has_many: schedules
和 Schedule 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'=
在您的控制器中评论过滤器:
#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
我有自定义控制器操作 #load
和 #save
名为 schedules_controller
。我还有 Boat Model,其中 Boat has_many: schedules
和 Schedule 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'=
在您的控制器中评论过滤器:
#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