有一些配置,想通过WEB界面管理。但没有必要存入数据库,用yml文件方便些。
读文件比较简单:
def self.get_config
YAML::load(File.read(Rails.root.to_s + '/config/points_base_rules.yml'))
end
读取对象之后,就可以显示在form上了,编辑之后,put 到server, server再将数据写入yml
写文件方法:
def self.save(rules)
result = true
begin
File.open(Rails.root.to_s + '/config/points_base_rules.yml', 'w') { |f|
YAML.dump(rules, f)
}
rescue => err
logger = Logger.new(Rails.root.to_s + '/log/err.log')
logger.error err
logger.close
result = false
end
result
end
这种方法在需要写的数据中包含中文时,会出现异常:
<Encoding::UndefinedConversionError: "\xE7" from ASCII-8BIT to UTF-8>
后来google了好久,终于找到解决方法:
gem install ya2yaml
在Gemfile里:
gem 'ya2yaml'
然后修改写文件的方法:
require 'ya2yaml'
def self.save(rules)
result = true
begin
File.open(Rails.root.to_s + '/config/points_base_rules.yml', 'w') { |f|
#YAML.dump(rules, f)
f.puts rules.ya2yaml
}
rescue => err
logger = Logger.new(Rails.root.to_s + '/log/err.log')
logger.error err
logger.close
result = false
end
result
end
问题解决!
分享到:
相关推荐
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
Ruby on Rails入门经典-例子,有很多rails工程实例。
Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置
Ruby on Rails的性能调优方案研究,张淼森,杨杰,Ruby on Rails 框架自它提出之日起就受到广泛关注。由于Rails框架基于MVC(Model-View-Controller) 模型,可以清楚地将模型层的代码与控制层的应
Ruby on Rails 初体验--北大青鸟教师专题讲座PPT 想学Ruby的赶快下载看看。 Ruby--目前最快速开发工具
原文是Web版本,已经导出成PDF版本供大家查看。原版是英文版的《Ruby on Rails Tutorial》,特别适合有其他语言开发经验的Rails入门。
Ruby On Rails 框架自它提出之日起就受到广泛关注,在“不要重复自己”,“约定优于配置”等思想的指导下,Rails 带给 Web 开发者的是极高的开发效率。 ActiveRecord 的灵活让你再也不用配置繁琐的 Hibernate 即可...
Ruby.on.Rails.Tutorial,Learn.Web.Development.with.Rails,Third.Edition-中文版 文字版.pdf 个人收集电子书,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!
2-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏源码On Rails Shooter Template 1.202-94街机外星风格射击游戏...
Ruby on Rails与MongoDB 您可以在MongoDB的帮助下轻松创建Rails项目。在Mac上安装MongoDB 启动MongoDB服务器mongodMongodb数据目录权限 sudo chmod 777 /data/db创建一个Rails项目使用'--skip-active-record'开关...
rails-dev-box, 面向 Ruby on Rails 核心开发的虚拟机 用于 Ruby on Rails 核心开发的虚拟机简介注意:这个虚拟机不是为 Rails 应用程序开发而设计的,只是为。 这个项目自动设置开发环境,以便在 Ruby on Rails ...
Ruby on Rails Tutorial(3rd-1.0.2)适合初学者,详细。
ruby on rails视频教程 链接:https://pan.baidu.com/s/10eKsJLllLySXk-b5muV_Qw 密码见文件
This book is for anyone who wants to develop online applications using Ruby and Rails. All you have to know is HTML to read this book profitably. On the other hand, Beginning Ruby on Rails is all ...
“It is impossible not to notice Ruby on Rails. It has had a huge effect both in and outside the Ruby community... Rails has become a standard to which even well-established tools are comparing ...
Ruby on Rails Tutorial 原书第三版中文版
1.将.rb文件和.rhtml文件都保存为utf-8格式; 2.在/app/Controller/application.rb中增加如下代码: ruby 代码 代码如下:before_filter :set_charset def set_charset headers[“Content-Type”] = “text/html; ...
Ruby on Rails 4.0 Guide 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书