Hi community, for my application, I decided to use a configuration based on YAML. For this purpose I installed a plugin called "app_config" (I think there are at least two plugins called "app_config", so I hope that plugin doesn't cause my problem). I wrote a "settings" controller, so that non-developers can easily change config values. I think I post the code here (It's only my first approach to see if this approach is a good idea, there is still a lot to improve):
class SettingsController < ApplicationController before_filter :authorise
def index redirect_to :action => "list" end
def list @changeable = YAML::load_file "#{RAILS_ROOT}/config/ change_settings.yml" @settings = Settings.params.to_a.flatten & @changeable.to_a.flatten end
def update redirect_to_404 unless request.xhr? path_to_settings_yml = RAILS_ROOT + "/config/settings.yml" y = YAML::load_file path_to_settings_yml changeable = YAML::load_file "#{RAILS_ROOT}/config/ change_settings.yml" y["common"][params[:key]] = params[:value] File.open(path_to_settings_yml, "w") { |f| f.write(y.to_yaml) } render :nothing => true end end
The "update" action is called via link_to_remote: <%= link_to_remote "Update", {:url => { :action => "update", :key => setting}, :with => "'value=' + prompt('Wert', '')" , :method => "put" }, {:id => "update_#{setting}"} %>
"Settings" is an instance of the "AppConfig" class provided by the plugin: class AppConfig def initialize(file = nil) @sections = {} @params = {} use_file!(file) if file end def use_file!(file) begin hash = YAML::load(ERB.new(IO.read(file)).result) @sections.merge!(hash) {|key, old_val, new_val| (old_val || new_val).merge new_val } @params.merge!(@sections['common']) rescue; end end def use_section!(section) @params.merge!(@sections[section.to_s]) if @sections.key? (section.to_s) end def method_missing(param) param = param.to_s if @params.key?(param) @params[param] else raise "Invalid AppConfig Parameter " + param end end
def params @params end end
This code works - it's not very good, but it works. But after a value is written, when I reload the page, the value don't change - although it's successfully changed in the settings.yml. I'm using the server provided by Rails 2.1 (started via script/server): => Booting Mongrel (use 'script/server webrick' to force WEBrick) => Rails 2.1.0 application starting on http://0.0.0.0:3000 => Call with -d to detach => Ctrl-C to shutdown server ** Starting Mongrel listening at 0.0.0.0:3000 ** Starting Rails with development environment... ** Rails loaded. ** Loading any Rails specific GemPlugins ** Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart). ** Rails signals registered. HUP => reload (without restart). It might not work well. ** Mongrel 1.1.4 available at 0.0.0.0:3000 ** Use CTRL-C to stop.
After I restart the server, the changed value appears on the page. It seems to me that the server is using some kind of caching mechanism, but it is also possible that my browser causes the problem (I tested it with Safari and Firefox). I hope anybody can help me to solve this problem, it's a very annoying one.
Thank you very much in advance Christoph