How to create a form for the rails-settings plugin

后端 未结 1 1563
臣服心动
臣服心动 2020-12-16 21:57

I have a Rails 3 App that has needs some user defined settings. I would like to use this https://github.com/ledermann/rails-settings plugin. I have it working in the rails

相关标签:
1条回答
  • 2020-12-16 22:27

    What I did is add dynamic setters/getters to my User class as such

    class User < ActiveRecord::Base
    
      has_settings
    
      def self.settings_attr_accessor(*args)
        args.each do |method_name|
          eval "
            def #{method_name}
              self.settings.send(:#{method_name})
            end
            def #{method_name}=(value)
              self.settings.send(:#{method_name}=, value)
            end
          "
        end
      end
    
      settings_attr_accessor :color, :currency, :time_zone
    
    end
    

    With that, you can use "color" just like any other attribute of your User model. Also it's very simple to add more settings, just add them to the list

    0 讨论(0)
提交回复
热议问题