By default Rails assumes that you have your files precompiled in the production environment, if you want use live compiling (compile your assets during runtime) in production you must set theconfig.assets.compile to true.

# config/environments/production.rb
...
config
.assets.compile = true
...

You can use this option to fallback to Sprockets when you are using precompiled assets but there are any missing precompiled files.

If config.assets.compile option is set to false and there are missing precompiled files you will get an "AssetNoPrecompiledError" indicating the name of the missing file.

____________________________________________________

You will get better performance in production if you set config.assets.compile to false in production.rb and precompile your assets. You can precompile with this rake task:

bundle exec rake assets:precompile

If you are using Capistrano, version 2.8.0 has a recipe to handle this at deploy time. For more info, see the "In Production" section of the Asset Pipeline Guide: http://guides.rubyonrails.org/asset_pipeline.html

http://stackoverflow.com/questions/7275636/rails-3-1-0-actionviewtemplateerrror-application-css-isnt-precompiled

 

有时候我们需要形象的展示实体间的关系(Entity-Relationship Diagrams)

rails-erd 这个gem 可以帮我们实现:

首先安装Graphviz
% brew install cairo pango graphviz # Homebrew on Mac OS X

% sudo port install graphviz # Macports on Mac OS X

% sudo aptitude install graphviz # Debian and Ubuntu 

然后在开发环境中使用

group :development do
  gem "rails-erd"
end
安装
% bundle install 
生成PDF
% rake erd

然后在项目根目录下就会生成ERD.pdf


http://rails-erd.rubyforge.org/

 

ModuleActionView::Helpers::SanitizeHelper


strip_tags(html)

Strips all HTML tags from the html, including comments. This uses the html-scanner tokenizer and so its HTML parsing ability is limited by that of html-scanner.

Examples

strip_tags("Strip <i>these</i> tags!")
# => Strip these tags!

strip_tags("<b>Bold</b> no more!  <a href='more.html'>See more here</a>...")
# => Bold no more!  See more here...

strip_tags("<div id='top-bar'>Welcome to my website!</div>")
# => Welcome to my website!
# File actionpack/lib/action_view/helpers/sanitize_helper.rb, line 83
def strip_tags(html)
  self.class.full_sanitizer.sanitize(html).try(:html_safe)
end

ActionController::Base.helpers.strip_tags

 

ROR开发小技巧

2011.12.25

ruby-1.9.2-p180 :055 > File.join("lib/","ruby","gem.rb")
 => "lib/ruby/gem.rb"

ruby-1.9.2-p180 :058 > [1,2,3] - [1,2]
 => [3] 

ruby-1.9.2-p180 :059 > [1,2,3] & [1,2]
 => [1, 2] 

ruby-1.9.2-p180 :060 > [1,2,3] + [1,2,3,4]
 => [1, 2, 3, 1, 2, 3, 4]

ruby-1.9.2-p180 :066 > %w(a d c d 1 2 3)
 => ["a", "d", "c", "d", "1", "2", "3"]

ruby-1.9.2-p180 :074 > Hash["a",1,"b",2]
 => {"a"=>1, "b"=>2} 

ruby-1.9.2-p180 :075 > Hash[*["a",1,"b",2]]
 => {"a"=>1, "b"=>2} 

ruby-1.9.2-p180 :075 > Array("a".."z").select.with_index {|x,i| i%3 == 2}
 => ["c", "f", "i", "l", "o", "r", "u", "x"]

# coding:utf-8
# unicode转义
"" << 0x6f22 #=> 漢

class Object
  # returns instance methods of current object's class and its singleton methods
  def lm # abbr of local methods
    self.methods.sort - self.class.superclass.instance_methods
  end
end

参考:http://ruby-china.org/topics/541
 
项目里有用到meta_search与acts-as-taggable-on这两个gem,后台录入的MM需要有通过标签查询的功能
meta_search的文档好像没有提到类似这种需求的解决方法,GOOGLE之:

找到如下解决方法
http://stackoverflow.com/questions/5766427/combining-meta-search-with-acts-as-taggable-on
Post.metasearch({:title_or_tag_taggings_tag_name_contains => params[:search]})

在我项目中,acts-as-taggable-on :times 相应改成
<%= f.select :time_taggings_tag_name_contains, [["请选择",""]]+Tag::TIMES %>

在console中测试能达到效果,但是生成的SQL,有些复杂
ruby-1.9.2-p180 :004 > Solution.metasearch({:time_taggings_tag_name_contains => "XXX"})
  Solution Load (6.6ms)  SELECT `solutions`.* FROM `solutions` LEFT OUTER JOIN `taggings` ON `taggings`.`taggable_id` = `solutions`.`id` AND taggings.tagger_id IS NULL AND taggings.context = 'times' AND `taggings`.`taggable_type` = 'Solution' LEFT OUTER JOIN `tags` ON `tags`.`id` = `taggings`.`tag_id` WHERE (`tags`.`name` LIKE '%XXX%')


有个问题:
我发布到服务器后测试报错:
Mysql2::Error: Unknown column 'tags.id' in 'on clause': SELECT COUNT(DISTINCT `solutions`….......
我比较了一下本机与服务器的MySQL版本:
本机:
mysql5  Ver 14.14 Distrib 5.1.53, for apple-darwin10.5.0 (i386) using readline 6.1
服务器:
mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (x86_64) using readline 6.1
不知道是不是因为服务器版本的差异导致的这个问题。
 

Home Blog Delicious Github Flickr About Contact

© Miclle.Zheng . Powered by Forest Chalet