Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Controller - Inherit a Custom Action

I have DRY'd up the controller code using inheritance.

class MastersController < ApplicationController
  def index
    ...
    ...
  end

  ...
  ...
end

class ItemsController < MastersController
end

Now I have added a custom action to the MastersController

class MastersController < ApplicationController
  def index
    ...
    ...
  end

  ...
  ...

  def clone
    ...
    ...
  end
end

I have added the routes for item

resources :items do
  get :clone
end

Now when I try to access myapp.dev/items/1/clone, I get the error

AbstractController::ActionNotFound at /items/1/clone
The action 'clone' could not be found for ItemsController

Screenshot

If I add the 'clone' action in ItemsController the error goes away.

class ItemsController < MastersController
  def clone
    ...
    ...
  end
end

How do I abstract a custom action in Rails Controller?

like image 836
Geordee Naliyath Avatar asked Mar 26 '14 13:03

Geordee Naliyath


1 Answers

You can use it like this

   def clone
     super
   end

in your child Controller

like image 72
dsounded Avatar answered Oct 16 '22 14:10

dsounded