开发者

How to create a form for the rails-settings plugin

开发者 https://www.devze.com 2023-04-05 04:13 出处:网络
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 console开发者_运维技巧.But I a

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 console开发者_运维技巧. But I am having trouble getting working in a form. Do I use fields_for & attr_accessible? If so I am having no luck.

I need to add settings for two Models:

For example, settings that are specific to a User,

user = User.find(123)
user.settings.color = :red
user.settings.color
# => :red

user.settings.all
# => { "color" => :red }

(The above works fine for me in the console.)

but I need to administer them through a standard web form. I'd love to know how others are handling this.

Thanks.


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

精彩评论

暂无评论...
验证码 换一张
取 消