摘要:#### instance_eval从名字上看,它的调用者必须是一个实例:class Aenda = A.newa.instance_eval do self #===>a def method1 puts "it's a's instance method, only for a" ...
阅读全文
随笔分类 - ror
学习心得和,一些错误的积累!
摘要:nginx + unicorn 部署 rails 项目,顾名思义要先安装nginx和unicorn一 安装安装nginx: sudo apt-get install nginx安装unicorn: gem install unicorn二 配置1 在你项目的config文件夹下添加一个unicorn...
阅读全文
摘要:class Customer < ActiveRecord::Base has_many :ordersendclass Order < ActiveRecord::Base belongs_to :customerend如上代码两个model在做如下查询的时候:c = Customer.fir...
阅读全文
摘要:Rake is a simple ruby build program similar to make. There are many versions of Rake are available. If you are using Rake-0.9.x you might come to the error, “wrong number of arguments (1 for 0) (ArgumentError)”. While the same piece of code runs without error on Rake-0.8.x. Here you will find the tw
阅读全文
摘要:Some time you may need to uninstall all the installed Ruby gems. Uninstalling individual gems is tedious job. Here you will find how to uninstall all the installed Ruby gems using single command.First, we need a list of installed gems. Its easier to get list of installed gems by using the command:ge
阅读全文
摘要:Gemfile gem 'sunspot_rails'gem 'sunspot_solr'然后执行 bundle之后就是rails g sunspot:solr:install生成一个config/sunspot.yml文件,不需要管理这个生成文件然后执行bundle exec rake sunspot:solr:startclass Post < ActiveRecord::Base searchable do text :title, :body text :comments do comments.map { |comment| comment.bo
阅读全文
摘要:strftime format codes for Ruby on RailsYear%Y year with century 2011%y year without century 11%C century number (year divided by 100) 20Month%B full month name January%b abbreviated month name Jan%h same as %b Jan%m month as number (01-12)Week%U week number of the year, Sunday as first d...
阅读全文
摘要:首先是不为空validates :name,presence:{message:'blank is not allowed'}唯一性:validates :name,uniqueness:{message:'already exist!',case_sensitive: false}#case_sensitive区分大小写长度:validates :name,length:{in: 6..20,message:'must in 6-20 byte'} 格式:validates :name,format:{with: /[a-z]/,message
阅读全文
摘要:前天在使用mongoid的时候用了class Article include Mongoid::Document field :name field :content field :published_on, type: Dateend 在相应的视图中添加了一些对应属性的试图<%= f.date_select :published_on %> 当创建一个article成功后,在展现的html中published_on对应的是空的,后来在数据库中查看了一下,插入进去了,但是插入的不是published_on而是变成了三个属性published_on(1i) 和 published_o
阅读全文
摘要:每天学一点,天天都是进步!content_for在次模板中使用,比如index.html.erb或者其他,对应的yield在主模板中使用,对于index.html.erb来说的application.html.erb中使用。在index.html.erb中有如下代码:<% content_for(:list) do %> <ol> <% for i in 1..5 %> <li>I'm <%= i %> !</li> <% end %> </ol><% end %> 然后在app
阅读全文
摘要:命令行 rake db:*****script/generate model task name:string priority:integer script/generate migration add_description_to_task description:string script/generate migration remove_description_from_task description:string 数据类型 引用# :string, :text, :integer, :float,:decimal, :datetime, :timestamp, :time, :d
阅读全文
摘要:gem install heroku foreman当你创建完一个项目后并且通过$git init$git add .$git commit -m "Init"$git remote add origin https://github.com/<username>/<项目名称>.git$git push -u origin master提交到GitHub上然后在用$git push heroku master$heroku create --stack cedar $heroku run db:migrate 这样就完成了。一定要注意在你的Gemfi
阅读全文
摘要:bulk參數 :bulk => true可以讓變更資料庫欄位的Migration更有效率的執行,如果沒有加這個參數,或是直接使用add_column、rename_column、remove_column等方法,那麼Rails會拆開SQL來執行,例如: change_table(:users) do |t|t.string :company_namet.change :birthdate, :datetime end 會產生: ALTER TABLE `users` ADD `im_handle` varchar(255) ALTER TABLE `users` ADD `compan.
阅读全文
浙公网安备 33010602011771号