如何从子控制器访问方法 - 从一开始 - 知道哪个子控制器?

时间:2013-09-10 10:00:08

标签: ruby-on-rails ruby routes subclass

我的自定义方法webshops#reset实际上从未从webshops_controller调用,因为它不存在Webshop个对象。相反,我有类似PrestashopWebshopMagentoWebshop等的子类

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(所有子类使用相同的webshops.js.coffee通过ajax调用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'

1 个答案:

答案 0 :(得分:0)

这可能有效:

class WebshopsController < InheritedResources::Base
  custom_actions :resource => :reset

来自inherited_resources docs

  

从1.2版开始,Inherited Resources允许您定义自定义   控制器中的动作:

class ButtonsController < InheritedResources::Base
  custom_actions :resource => :delete, :collection => :search
end
     

此代码创建   删除和搜索控制器中的操作(它们的行为类似于show和   因此指数行动)。此外,它将产生delete_resource_{path,url}   和search_resources_{path,url}网址助手。