rails tips

来源:互联网 发布:宁波每日成交数据 编辑:程序博客网 时间:2024/04/24 20:01

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-分页gemgem '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


14.使用指令在dos窗口中来创建表和添加数据

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  endend

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中是什么变量





0 0