How to create a form for rails-settings plugin

I have a Rails 3 application that requires certain user preferences. I would like to use this plugin https://github.com/ledermann/rails-settings . I work in rails console. But it's hard for me to work in shape. Should fields_for and attr_accessible be used? If so, I’m out of luck.

I need to add settings for two models:

For example, user-specific settings,

user = User.find(123) user.settings.color = :red user.settings.color # => :red user.settings.all # => { "color" => :red } 

(This works fine for me in the console.)

but I need to administer them through a standard web form. I would like to know how others deal with this.

Thanks.

+7
source share
1 answer

What I did was 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 

In this case, you can use the "color", like any other attribute of your user model. It’s also very easy to add additional settings, just add them to the list

+13
source

All Articles