I'm writing a plugin on Redmine.
I want to add a new method inside a existing controller of Redmine. The controller name is Repositories.
I wrote in repositories.rb the following code:
class RepositoriesController < ApplicationController
def exec_client
...
end
end
In routes.rb I put:
match '/projects/:id/repository', :controller => 'Repositories', :action => 'exec_client', :via => :post
In my view navigation.html.erb I wrote:
<%= button_to_function l(:gerar_build_project), remote_function(:action => 'exec_client', :controller => 'Repositories')%>
The code of class RepositoriesController was originally written on the file repositories_controller.rb.
But, when I click in my button I've created in my view, I get the following message:
AbstractController::ActionNotFound (The action 'exec_client' could not be found for RepositoriesController):
What's going wrong?
To extend a class in a Redmine plugin and add new methods, you need to follow these steps:
In the path plugin/lib/client I created the file client.rb
#encoding: UTF-8
module RepositoriesPatch
require_dependency 'repositories_controller'
def self.included(base)
base.send(:include, InstanceMethods)
end
end
module InstanceMethods
require_dependency 'repositories_controller'
def exec_client
[....]
end
end
Rails.configuration.to_prepare do
RepositoriesController.send(:include, RepositoriesPatch)
end
Above I created a patch which a new function to repositores_controller and have inserted it using the command .send
In init.rb I put:
Rails.configuration.to_prepare do
RepositoriesController.send(:include, RepositoriesPatch)
end
The rest stood the same. Hope this can be useful to someone. Thanks!