如何创建轨道,设置插件形式 [英] How to create a form for the rails-settings plugin

查看:174
本文介绍了如何创建轨道,设置插件形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3应用程序,需要有一些用户定义设置。我想利用这个 https://github.com/ledermann/rails-settings 插件。我有它在轨控制台的工作。但我有麻烦的形式工作。难道我用fields_for和放大器; attr_accessible?如果是这样,我有没有运气。

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.)

但我需要通过一个标准的Web表单来管理它们。我很想知道别人是怎么处理这一点。

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

感谢。

推荐答案

我所做的是添加动态的setter / getter方法​​,以我的用户类,例如

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

这篇关于如何创建轨道,设置插件形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆