Adding actions to the SettingsController on a plugin
Added by Enrique Garcia over 16 years ago
I'm developing a new plugin that will need a "settings" view.
On that view I'll need to make a couple drop-downs; things like the Project List and the Custom Field for project lists.
If I was developing a normal app, I would just update the Settings controller and add a @pictures and @custom_fields instance variables on a before_filter. But since this is a custom app
I've tried monkeypatching SettingsController like this:
# vendor/plugins/my_plugin/app/controllers/SettingsController.rb
class SettingsController < ApplicationController
before_filter :calculate_project_custom_fields, :only => :plugin
private
def calculate_project_custom_fields
if params[:id] == 'my_plugin'
@projects = Projects.all
@project_custom_fields = CustomField.find_all_by_type('ProjectCustomField')
end
end
end
This allows me to reach the Settings page, where I've a form that uses @projects and @project_custom_fields to modify settings (tags reference "settings[something][something]"). When I POST this form, however, I get the following error:
I've googled this and found one reference saying that this could be solved by making SettingsController "unloadable" - but then I read that this was a deprecated method. And I wouldn't know how to make it unloadable anyway - so I'm stuck.
I guess I could calculate @pictures and @custom_fields directly in the view, but that sounds so un-rails-y.
What's the correct way to get @projects and @project_custom_fields available for my view ?
Replies (3)
RE: Adding actions to the SettingsController on a plugin
-
Added by Enrique Garcia over 16 years ago
I'm still not able to find a good solution for this.
By they way, am I not receiving answers because I posted on the wrong forum? Should I post to the help forum instead?
Thanks a lot,
Enrique García
RE: Adding actions to the SettingsController on a plugin
-
Added by Jean-Baptiste Barth over 16 years ago
- add unloadable to the class being patches
- add a require_dependency 'the_class_you_are_patching' at the top of your patch
- require explicitly the file in your init.rb, in a "config.to_prepare" block or "Dispatcher.to_prepare" (it's the same..)
Regarding the last advice, if I were you I wouldn't put the file in app/controllers, but in a custom directory of your choice in lib/. But maybe it has no impact, I let you do your own tests.
I don't have the time to give you much explanations on this, maybe I'll add a wiki page about this problem. You should read Eric's wiki page about these things in the wiki, here: Plugin_Internals
RE: Adding actions to the SettingsController on a plugin
-
Added by Enrique Garcia over 16 years ago
Hi Jean Baptiste,
I read the file some time ago, when I developed my first plugin. Didn't realize it had been updated with controller-specific information.
I'll try the extend-by-a-module approach, and apply those 3 things if I still get errors.