我的自定义方法webshops#reset
从未真正从 中调用webshops_controller
,因为它不Webshop
存在任何对象。相反,我有子类PrestashopWebshop
,如MagentoWebshop
, 等。
Myapp::Application.routes.draw do
...
resources :webshops
resources :magento_webshops
resources :prestashop_webshops
get "api/webshop/:id/reset" => "webshops#reset"
...
end
以下是来自子类控制器的方法。
class MagentoWebshopsController < WebshopsController
def scoped_webshops
"magento_webshops"
end
end
当我从子类控制器magento_webshops_controller
中调用index
我可以访问的操作时scoped_webshops
。但是当我调用自定义方法时,reset
我无法访问这些子类方法。
class WebshopsController < InheritedResources::Base
respond_to :html, :json
before_filter :authenticate_user!
def index
@webshops = current_user.send(scoped_webshops)
end
def reset
webshop = Webshop.find(params[:id])
@webshops = current_user.send(scoped_webshops)
respond_to do |format|
format.html {
...
}
format.js
end
end
end
那么,我该如何调用api/webshop/:id/reset
(所有子类都使用相同的通过ajaxwebshops.js.coffee
调用)?reset
我猜整个问题是由webshops_controller
直接工作引起的,而没有从子类调用它的动作。然而,第一行 ( webshop = Webshop.find(params[:id])
) 应该以某种方式让我们能够确定应该使用哪个子类,因为它返回一个子类对象......
更新:
这是服务器响应:
Started GET "/api/webshop/522ede3983c336b7d600000f/reset?time_string=5&_=1378809004554" for 127.0.0.1 at 2013-09-10 12:30:14 +0200
Processing by WebshopsController#reset as JS
Parameters: {"time_string"=>"5", "_"=>"1378809004554", "id"=>"522ede3983c336b7d600000f"}
MOPED: 127.0.0.1:27017 QUERY database=feedbackzilla_development collection=webshops selector={"_id"=>"522ede3983c336b7d600000f"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4311ms)
MOPED: 127.0.0.1:27017 QUERY database=feedbackzilla_development collection=users selector={"$query"=>{"_id"=>"522887dc83c3368361000964"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.5710ms)
MOPED: 127.0.0.1:27017 QUERY database=feedbackzilla_development collection=webshops selector={"_id"=>"522ede3983c336b7d600000f"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4909ms)
MOPED: 127.0.0.1:27017 UPDATE database=feedbackzilla_development collection=webshops selector={"_id"=>"522ede3983c336b7d600000f"} update={"$set"=>{"rebuild_days"=>5, "updated_at"=>2013-09-10 10:30:14 UTC}} flags=[] (0.1259ms)
Completed 500 Internal Server Error in 86ms
NameError - undefined local variable or method `scoped_webshops' for #<WebshopsController:0x007fb710accc08>:
app/controllers/webshops_controller.rb:50:in `reset'