ITeye博客

RunUpwind的博客

RunUpwind · 共 32 篇文章

checking for libxml/parser.h... no

在linux安装 savon 时,遇到这个错误。 yum install libxml2-devel yum install libxslt-devel 之后,再gem install savon 成功! 参考: http://stackoverflow.com/questions/5881156/git-bundle-install-error-with-nokogiri-libxms2-is-missing

2011-12-06 11:41 · ruby on rails · 评论(0)

ruby string replace用gsub

"Hello World".gsub("Hello", "Bye")

2011-07-21 17:16 · ruby on rails · 评论(0)

Encoding::UndefinedConversionError: "\xE5" from ASCII-8BIT to UTF-8

在做编码转换时,遇了这个问题。原代码如下: str.encode('UTF-8') if str.encoding != 'UTF-8' 所以就遇到了错误:Encoding::UndefinedConversionError: "\xE5" from ASCII-8BIT to UTF-8 后来看了https://github.com/lassebunk/webcam_app/issues/1 将代码修改为以下, str.force_encoding('UTF-8') if str.encoding != 'UTF-8' 这只是改了字符串的encoding值,实际数据并未转码。 问题等待解决…

2011-07-21 11:59 · ruby on rails · 评论(0)

linux安装rmagick经验总结

我的环境: ruby -v 其中一台(32位):ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux] 另一台(64位):ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux] rails -v Rails 3.0.5 安装rmagick,首先要安装ImageMagick ,用yum安装的可能是6.28,rmagick2基于6.4以上,所以要到官网下载源代码,按网页提示,编译,安装: http://www.imagemagick.org/script/install-sourc…

2011-07-18 18:09 · ruby on rails · 评论(0)

rails 利用easy_captcha生成图片验证码

gem install easy_captcha 文档在这里: http://rubydoc.info/gems/easy_captcha/0.4.2/frames

2011-07-11 18:37 · ruby on rails · 评论(0)

基于windows+ruby1.91安装rmagick

如题,试用N种方法,终于找了一个可用的方法: http://www.waydotnet.com/blog/2010/02/rmagick-on-ruby-1-9-1=-i386-mingw32-work-d/ 其中的环境变量设置那一步很重要,我第一次漏掉了,导致没有成功。

2011-07-11 18:35 · ruby on rails · 评论(0)

to_json 中增加自定义的属性字段

参考:http://stackoverflow.com/questions/5111165/accessing-virtual-attribute-from-json 原本我这样写: @user.to_json(:only => [:id, :name, :pay_points]) 后来我想增加一个属性,它不是一个数据库字段,是在@user中定义的一个方法: def points_value ...... end 我将to_json输出改成了: @user.to_json(:only => [:id, :name, :pay_points, :points_value]) 但结果没有输出po…

2011-07-06 17:59 · ruby on rails · 评论(0)

rails created_at时区不对

参见:http://stackoverflow.com/questions/2512450/time-now-created-at-are-different-ruby-on-rails 将config/application.rb 中的 config.time_zone = 前的注释去掉。改为: config.time_zone = 'Beijing' 即可。 2011-07-06更新: 虽然页面上显示的时间对了,但数据库存储的时间还是UTC时间,这会有些不便,怎样让数据库存储本地时间呢? config.active_record.default_timezone = :local 把这句…

2011-06-30 16:21 · ruby on rails · 评论(0)

把表结构写进model(annotate_models,annotate)

在做ROR开发时,经常要去查schema.rb, 确实有些不便。 《Web开发敏捷之道-应用Rails进行敏捷Web开发-第三版》中提到一个插件可将数据库信息添加到model顶端:annotate_models。 因为书中所讲内容已是过去时了,已经找不到那个plugn了。 google得知:现在有这样的GEM了: https://github.com/sagework/annotate_models 首先安装这个GEM: gem install annotate 然后在Gemfile里添加(重要): gem 'annotate' 到你的ROR app目录下,执行: annotate 搞定。默…

2011-06-29 14:02 · ruby on rails · 评论(0)

ruby on rails 读写yml文件(中文utf-8问题)

有一些配置,想通过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_rul…

2011-06-29 11:41 · ruby on rails · 评论(0)

Ruby Net::HTTP cheat sheet

转自:http://augustl.com/blog/2010/ruby_net_http_cheat_sheet I always have to look up how to use Net::HTTP , and I never find what I’m looking for. Behold, a cheat sheet! A basic request require "net/https" require "uri" uri = URI.parse("http://google.com") http = Net::HTTP.new(uri.host, uri.port) req…

2011-06-22 19:03 · ruby on rails · 评论(0)

rails异常(错误)处理

方法1:http://www.uedidea.com/rails%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86.html def add_to_cart product = Product.find(params[:id]) rescue ActiveRecord::RecordNotFound #拦截Product.find()异常 logger.error("Attempt to access invalid product #{params[:id]}") #日志记录 flash[:notice] = "Invalid product" #notice 存放提…

2011-06-22 15:04 · 评论(0)

rails 如何清除clear session

参照:http://stackoverflow.com/questions/684457/how-do-you-clear-a-single-entry-from-a-ruby-on-rails-session http://stackoverflow.com/questions/2405635/how-to-empty-destroy-a-session-in-rails 清除session中的单个对象: session[:foo] = nil session.delete :foo session.data.delete :foo 清除当前session中的所有对象: reset_ses…

2011-06-22 13:50 · ruby on rails · 评论(0)

rails中如何添加自定义的请求格式

rails默认request的格式是 text/html. 同时还支持 xml 和 json. 如果要添加一个格式如.api, 访问路径:http://..../login.api, 该如何做? 参照:http://stackoverflow.com/questions/2456219/add-a-custom-format-in-rails-that-will-work-with-respond-to # add in Environment.rb Mime::Type.register "api/json", :api 在controller中,可以用request.format 取得值…

2011-06-22 11:28 · ruby on rails · 评论(0)

rails接口验证:HTTP Basic Access Authentication

server端: 参照:http://railscasts.com/episodes/82-http-basic-authentication products_controller.rb before_filter :authenticate protected def authenticate authenticate_or_request_with_http_basic do |username, password| username == "foo" && password == "bar" end end client端: 参照:http://www.iteye.com/topic…

2011-06-22 10:05 · ruby on rails · 评论(0)