0

我的自定义方法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'
4

1 回答 1

0

这可能有效:

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

继承的资源文档

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

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

此代码在控制器中创建删除和搜索操作(它们的行为类似于相应的显示和索引操作)。此外,它还会生成delete_resource_{path,url}search_resources_{path,url}url 助手。

于 2013-09-10T10:22:16.627 回答