1.在model 中,使用多語言支持的時候,會不識別
可以使用以下方式來加載:
validates :code, uniqueness: { message: I18n.t(:cus_code_msg) }
2.rails 中對一個字段做唯一限制
如上:
validates :字段名, uniqueness: { message: "返回的錯誤信息") }
3.現在最新的版本是 ruby-2.2.0 ,rails 4.2.0
rvm是一個用來管理ruby版本的工具
原來做的項目是 ruby-1.8.7,和ruby-2.2.0相比,有很大的變化,而且ruby從不向下兼容,所以要學習最新版本
4.ror項目的名稱和model不能有同名出現,否則會報錯,一些方法都不識別
5.
gem 'therubyracer', platforms: :ruby
gemfile中 這個gem要放開,默認是註釋掉的,否則會報js runtime error
6.項目要用到的一些gem 只需要在gemfile中添加 ,然後運行bundle install 如下
#2015-2-2-分頁gem
gem 'will_paginate'
然後 tools---》bundler-----》 install
7.設置默認的i18n文件,在config-----》environment.rb 中 添加如下代碼
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
I18n.default_locale = :zh #這是用來設置默認的i18n文件
8.在ror項目中,使用mysql數據庫,經常會用到日期轉換的問題,utc
.strftime('%Y-%m-%d %H:%M:%S')
即可轉換成常用的形式
9.linux 文件操作,skip all 全部忽略 ; merge all,整合
10. rails 2.3.8 ruby 1.8.7 創建新的application的時候,報以下錯誤
uninitialized constant ActiveSupport::Dependencies::Mutex (NameError)
解決方式:降低你的gem版本,
gem update --system 1.4.2
11.
列出所有可用的ruby版本
rvm list known
注:如果發現裏面沒有最新的ruby版本,請更新你的rvm版本,直接安裝即可
curl -sSL https://get.rvm.io | bash -s stable --ruby
系統會提示你,有多個rvm版本,reload即可
rvm reload
設置ruby默認版本
rvm --default use ruby-2.1.1
設置當前版本
rvm use ruby-2.1.1
12.如何在服務器上部署 ror項目
1.配置環境
2.copy整個項目目錄
3.如果安裝了thin,在項目根目錄下,運行thin start -p3002 &
13.ror中sqlite數據庫的使用,需要添加sqlite3的gem,然後在database.yml中配置
development:
adapter: sqlite3
database: db/develop.db
pool: 5
timeout: 5000
sqlite3 develop.db
進入數據庫操作界面;具體指令請百度15.在新版本的rails中,如何快速創建模塊對應的基本文件
需要在command窗口中,執行以下指令
rails generate scaffold 模塊name
rails generate scaffold rabbit code:string name:string remark:text buy_date:date
16.生成對應的數據庫表
rake db:migrate
參照以下網址: http://guides.rubyonrails.org/getting_started.html
17.一個根據數量顯示單複數的 方法?
pluralize(@rabbit.errors.count, "error")
18.設置rails4 項目的 i18n 國際化 配置(默認是en),在config==》application.rb 下
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# 本地化配置
config.i18n.available_locales = [:zh]#here
config.encoding = "utf-8"
config.active_record.default_timezone = :local
config.time_zone = 'Beijing'
config.i18n.default_locale = :zh #here
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
19.render partial 傳遞變量的用法,
具體請參照:http://hlee.iteye.com/blog/1084702
<1>rails view 層中能夠用單引號的不要用雙引號
<%= render 'area', :object => @areas %>
:object=>xxx,跟的是什麼變量,傳遞過去就是什麼變量。
<%= render :partial => "account", :locals => { :account => @buyer } %>
locals傳遞的是key-value形式,key是什麼,對應partial中是什麼變量