我已經使用繼承控制器代碼。Rails控制器 - 繼承自定義動作
class MastersController < ApplicationController
def index
...
...
end
...
...
end
class ItemsController < MastersController
end
現在我已經加入了自定義操作的MastersController
class MastersController < ApplicationController
def index
...
...
end
...
...
def clone
...
...
end
end
我已經添加了項目
resources :items do
get :clone
end
現在的路線,當我嘗試訪問myapp.dev/items/ 1 /克隆,我得到錯誤
AbstractController::ActionNotFound at /items/1/clone
The action 'clone' could not be found for ItemsController
如果我在ItemsController中添加'clone'操作,錯誤消失。
class ItemsController < MastersController
def clone
...
...
end
end
我如何抽象Rails中控制器的自定義操作?
另一個問題是圍繞同一主題http://stackoverflow.com/questions/22604996/rails-controller-inheritance-vs -concerns-and-mixins –
這是一個答案嗎? http://stackoverflow.com/questions/3998286/inheriting-and-routing-a-custom-action-from-applicationcontroller-in-rails(tadman的回答) –
是'克隆'低於任何私人方法?嘗試在'def clone'上面添加單詞'public'一行' – gabrielhilal