- 浏览: 51043 次
- 性别:
- 来自: 北京
-
最新评论
Rails版本区别
1. 脚本命令
旧的命令 新的用法
script/generate rails g
script/console rails c
script/server rails s
script/dbconsole rails db
2. 配置文件
rails2:config/environment.rb
- Rails::Initializer.run do |config|
- config.load_paths+=%W(#{RAILS_ROOT}/extras)
- config.gem"bj"
- config.gem"sqlite3-ruby" , :lib => "sqlite3"
- config.gem"aws-s3" , :lib => "aws/s3"
- config.plugins=[:exception_notification ]
- config.time_zone='UTC'
- end
Rails::Initializer.run do |config| config.load_paths += %W( #{RAILS_ROOT}/extras ) config.gem "bj" config.gem "sqlite3-ruby", :lib => "sqlite3" config.gem "aws-s3", :lib => "aws/s3" config.plugins = [ :exception_notification ] config.time_zone = 'UTC' end
rails3:config/application.rb
- module APP_NAME
- class Application<Rails::Application
- config.load_paths+=%W(#{RAILS_ROOT}/extras)
- config.plugins=[:exception_notification ]
- config.time_zone='UTC'
- end
- end
module APP_NAME class Application < Rails::Application config.load_paths += %W( #{RAILS_ROOT}/extras ) config.plugins = [ :exception_notification ] config.time_zone = 'UTC' end end
这样就变成了一种架构式的应用, 我们可以根据方便的对config进行操作
3. 路由
在rails3中, 已经的路由可以继续工作, 而新的路由方式更加简洁.
在 rails2 中:
- map.resources :posts do |post|
- post.resources:comments
- end
map.resources :posts do |post| post.resources :comments end
而在rails3中 , 表达更为形象:
- resources :posts do
- resources:comments
- end
resources :posts do resources :comments end
对于一些复杂的路由, rails2:
- post.resources :comments ,
- :member =>{ :preview => :post },
- :collection =>{ :archived => :get }
post.resources :comments, :member => { :preview => :post }, :collection => { :archived => :get }
在 rails3中可以这样表达:
- resources :comments do
- memberdo
- post:preview
- end
- collectiondo
- get:archived
- end
- end
resources :comments do member do post :preview end collection do get :archived end end
不够简洁? 我们还可以这样做:
- resources :comments do
- post:preview , :on => :member
- get:archived , :on => :collection
- end
resources :comments do post :preview, :on => :member get :archived, :on => :collection end
对于基本路由, rails2:
- map.connect 'login' , :controller => 'session' , :action => 'new'
map.connect 'login', :controller => 'session', :action => 'new'
那么在rails3中:
- match 'login' => 'session#new'
match 'login' => 'session#new'
对于具名路由, rails2:
- map.login 'login' , :controller => 'session' , :action => 'new'
map.login 'login', :controller => 'session', :action => 'new'
在rails3中:
- match 'login' => 'session#new' , :as => :login
match 'login' => 'session#new', :as => :login
对于程序根路由, rails2:
- map.root:controller=> 'users' ,:action=> 'index'
map.root :controller => 'users', :action => 'index'
rails3:
- root :to => 'users#index'
root :to => 'users#index'
对于遗留路由, rails2:
- map.connect ':controller/:action/:id'
- map.connect':controller/:action/:id.:format'
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format'
那么在rails3中写法更优雅:
- match ':controller(/:action(/:id(.:format)))'
match ':controller(/:action(/:id(.:format)))'
对于路由参数, rals2:
- map.connect '/articles/:year/:month/:day' , :controller => 'posts' , :action => 'index'
map.connect '/articles/:year/:month/:day', :controller => 'posts', :action => 'index'
rails3:
- match '/articles/:year/:month/:day' => "posts#index"
match '/articles/:year/:month/:day' => "posts#index"
那么对于存档请求, 比如rails2:
- map.connect '/articles/:year/:month/:day' , :controller => 'posts' , :action => 'index'
- map.connect'/articles/:year/:month' , :controller => 'posts' , :action => 'index'
- map.connect'/articles/:year' , :controller => 'posts' , :action => 'index'
map.connect '/articles/:year/:month/:day', :controller => 'posts', :action => 'index' map.connect '/articles/:year/:month', :controller => 'posts', :action => 'index' map.connect '/articles/:year', :controller => 'posts', :action => 'index'
在rails3中:
- match '/articles(/:year(/:month(/:day)))' => "posts#index"
match '/articles(/:year(/:month(/:day)))' => "posts#index"
指定请求方式, rails2:
- map.connect '/articles/:year' , :controller => 'posts' , :action => 'index' ,
- :conditions =>{ :method => :get }
map.connect '/articles/:year', :controller => 'posts', :action => 'index', :conditions => {:method => :get}
在rails3中:
- match '/articles/:year' => "posts#index" , :via => :get
- #或者更简单的:
- get'/articles/:year' => "posts#index"
match '/articles/:year' => "posts#index", :via => :get #或者更简单的: get '/articles/:year' => "posts#index"
对于跳转, rails3:
- match 'signin' , :to =>redirect( "/login" )
- match'users/:name' , :to =>redirect{|params| "/#{params[:name]}" }
- match'google' =>redirect( 'http://www.google.com/' )
match 'signin', :to => redirect("/login") match 'users/:name', :to => redirect {|params| "/#{params[:name]}" } match 'google' => redirect('http://www.google.com/')
路由约束: rails2中实际上使用了 :requirements 符号
- map.connect '/:year' , :controller => 'posts' , :action => 'index' ,
- :requirements =>{ :year =>//d{4}/}
map.connect '/:year', :controller => 'posts', :action => 'index', :requirements => { :year => //d{4}/ }
在rails3中:
- match '/:year' => "posts#index" , :constraints =>{ :year =>//d{4}/}
match '/:year' => "posts#index", :constraints => {:year => //d{4}/}
- :constraints =>{ :user_agent =>/iphone/}
- :constraints =>{ :ip =>/192/.168/.1/./d{1,3}/}
- constraints(:host =>/localhost/) do
- resources:posts
- end
- constraintsIpRestrictordo
- get'admin/accounts' => "queenbee#accounts"
- end
:constraints => { :user_agent => /iphone/ } :constraints => { :ip => /192/.168/.1/./d{1,3}/ } constraints(:host => /localhost/) do resources :posts end constraints IpRestrictor do get 'admin/accounts' => "queenbee#accounts" end
对于Rack应用, rails3:
- get 'hello' =>proc{|env|[200,{}, "HelloRack" ]}
- get'rack_endpoint' =>PostsController.action( :index )
- get'rack_app' =>CustomRackApp
get 'hello' => proc { |env| [200, {}, "Hello Rack"] } get 'rack_endpoint' => PostsController.action(:index) get 'rack_app' => CustomRackApp
4. Bundler与ActionController
一个典型的rails应用, 我们一般需要在 environment.rb 指定你的 gems:
- config.gem "haml"
- config.gem"chronic" , :version => '0.2.3'
config.gem "haml" config.gem "chronic", :version => '0.2.3'
然后我们运行 $ rake gems:install, 该命令会取得并下载然后安装编译这些gems到你的系统RubyGems目录中.
之后我们运行 $ rake gems:unpack:dependencise, 把这些gem打包到你应用程序的vendor/gems目录中去.
这样做产生的问题:
1. 它直接绑定到Rails中
2. 没有从本质上解决依赖问题
3. 运行时容易发生冲突
在rails3中, 使用了 bundle 命令:
直接在你的 gemfile 中指定你的 gem
- gem "haml"
- gem"chronic" , '0.2.3'
gem "haml" gem "chronic", '0.2.3'
然后运行 $ bundle, 该命令会会取得并下载然后安装编译这些gems
然后运行 $ bundle package 把gem源移到/vendor/cache中去.
这样rails应用中的gem与系统中的gem就不会相冲突.
一般的控制器语法:
- class UsersController<ApplicationController
- def index
- @users =User.all
- respond_todo |format|
- format.html
- format.xml{render:xml => @users .to_xml}
- end
- end
- def show
- @user =User.find(params[ :id ])
- respond_todo |format|
- format.html#show.html.erb
- format.xml{render:xml => @user }
- end
- end
- ...
class UsersController < ApplicationController def index @users = User.all respond_to do |format| format.html format.xml { render :xml => @users.to_xml } end end def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @user } end end ...
改进的语法:
- class UsersController<ApplicationController
- respond_to:html , :xml , :json
- def index
- @users =User.all
- respond_with(@users )
- end
- def show
- @user =User.find(params[ :id ])
- respond_with(@user )
- end
- ...
class UsersController < ApplicationController respond_to :html, :xml, :json def index @users = User.all respond_with(@users) end def show @user = User.find(params[:id]) respond_with(@user) end ...
5. ActionMailer
rails2: $ script/generatemailerUserMailerwelcomeforgot_password
这将创建app/models/user_mailer.rb
那么在rails3中:$ railsgmailerUserMailerwelcomeforgot_password
这将创建app/mailers /user_mailer.rb
在实现部分, rails2:
- def welcome(user,subdomain)
- subject'WelcometoTestApp'
- recipientsuser.email
- from'admin@testapp.com'
- body:user =>user, :subdomain =>subdomain
- end
def welcome(user, subdomain) subject 'Welcome to TestApp' recipients user.email from 'admin@testapp.com' body :user => user, :subdomain => subdomain end
- UserMailer.deliver_welcome(user,subdomain)
UserMailer.deliver_welcome(user, subdomain)
在rails3中:
- def welcome(user,subdomain)
- @user =user
- @subdomain =subdomain
- mail(:from => "admin@testapp.com" ,
- :to =>user.email,
- :subject => "WelcometoTestApp" )
- end
def welcome(user, subdomain) @user = user @subdomain = subdomain mail(:from => "admin@testapp.com", :to => user.email, :subject => "Welcome to TestApp") end
- UserMailer.welcome(user,subdomain).deliver
UserMailer.welcome(user, subdomain).deliver
相比rails2, 我们在rails3下实现一个mail要简单的多:
- class UserMailer<ActionMailer::Base
- default:from => "admin@testapp.com" ,
- :reply_to => "noreply@testapp.com" ,
- "X-Time-Code" => Time .now.to_i.to_s
- def welcome(user,subdomain)
- @user =user
- @subdomain =subdomain
- attachments['test.pdf' ]= File .read( "#{Rails.root}/public/test.pdf" )
- mail(:to => @user .email, :subject => "WelcometoTestApp" ) do |format|
- format.html{render'other_html_welcome' }
- format.text{render'other_text_welcome' }
- end
- end
- end
class UserMailer < ActionMailer::Base default :from => "admin@testapp.com", :reply_to => "noreply@testapp.com", "X-Time-Code" => Time.now.to_i.to_s def welcome(user, subdomain) @user = user @subdomain = subdomain attachments['test.pdf'] = File.read("#{Rails.root}/public/test.pdf") mail(:to => @user.email, :subject => "Welcome to TestApp") do |format| format.html { render 'other_html_welcome' } format.text { render 'other_text_welcome' } end end end
6. ActiveRelation 以及 ActiveModel
在rails2中, 我们经常使用下面的方法来进行查询:
- @posts =Post.find( :all , :conditions =>{ :published => true })
@posts = Post.find(:all, :conditions => {:published => true})
该方式将立即查询数据库然后返回Posts数组
而在rails3中:
- @posts =Post.where( :published => true )
@posts = Post.where(:published => true)
该方法不会查询数据库, 仅仅返回一个 ActiveRecord::Relation 对象, 然后:
- @posts =Post.where( :published => true )
- if params[ :order ]
- @posts = @posts .order(params[ :order ])
- end
- @posts . each do |p|
- ...#在这里进行查询,实现延迟加载
- end
@posts = Post.where(:published => true) if params[:order] @posts = @posts.order(params[:order]) end @posts.each do |p| ... #在这里进行查询, 实现延迟加载 end
对于命名范围, 在rails2中:
- class Post<ActiveRecord::Base
- default_scope:order => 'title'
- named_scope:published , :conditions =>{ :published => true }
- named_scope:unpublished , :conditions =>{ :published => false }
- end
class Post < ActiveRecord::Base default_scope :order => 'title' named_scope :published, :conditions => {:published => true} named_scope :unpublished, :conditions => {:published => false} end
而在rails3中:
- class Post<ActiveRecord::Base
- default_scopeorder('title' )
- scope:published ,where( :published => true )
- scope:unpublished ,where( :published => false )
- end
class Post < ActiveRecord::Base default_scope order('title') scope :published, where(:published => true) scope :unpublished, where(:published => false) end
对于查找方法, rails2:
-
Post.find(
:all
,
:conditions
=>{
:author
=>
"Joe"
},
:includes
=>
:comments
,
:order
=>
"title"
,
:limit
=>10)
- 在rails3:
Post.find(:all, :conditions => {:author => "Joe"}, :includes => :comments, :order => "title", :limit => 10)
- Post.where( :author => "Joe" ).include( :comments ).order( :title ).limit(10).<strong><spanstyle= "font-size:medium;" >all</span></strong>
Post.where(:author => "Joe").include(:comments).order(:title).limit(10).all
7. 跨站点脚本(XSS)
在rails2中, 一般我们输入一段文本的时候, 我们往往会这样写: <%= h @post.body %>
那么在rails3中, <%= @post.body %> 默认输出的是一段safe html, 如果想输出XSS, 可以在前面加上 raw
相关推荐
综上所述,从Rails 2到Rails 3的过渡不仅仅是版本号的简单递增,而是对整个框架的一次全面升级。Rails 3在脚本命令的调用、配置文件的结构以及路由定义的语法上都做了大量的优化,旨在提供更高效、更直观的开发体验...
介绍rails框架,版本是rails2点几的,不过思路差不多,具体区别可以去看官网
与稳定的Rails版本相比,Edge Rails可能包含未经过充分测试的新功能,因此对于开发环境而言,它提供了更多的探索和试验空间。使用Edge Rails的插件,如CRUD Generator 2,可以让你提前体验并利用到Rails的前沿技术。...
在Rails早期版本中,经常使用jQuery库来增强Ajax功能。jQuery简化了DOM操作和事件处理,并提供了`$.ajax()`、`$.get()`和`$.post()`等方法来发起Ajax请求。Rails可以通过`gem 'jquery-rails'`添加对jQuery的支持,并...
其v2.5版本更是对前一版本进行了优化,增强了性能,同时引入了更多现代化的设计元素和交互特性,使得后台管理界面更加直观易用。 接下来,我们将目光转向“Rails_Seed_Project”。在Ruby on Rails框架中,种子数据...
6. **版本控制**:使用Git等版本控制系统管理代码,确保部署的是特定的、已测试过的代码版本。 7. **服务器权限与用户管理**:理解sudo、root用户和非root用户权限的区别,设置适当的文件权限和用户组,确保安全的...
为了更清楚地将库与active_record_slave区别开来,我们还增加了主版本–但是,它在功能上是等效的。 介绍 active_record_replica将所有数据库读取重定向到副本实例,同时确保所有写入都转到主数据库。 active_...
这个名字暗示了源代码结构将遵循Git版本控制的习惯,包括.gitignore文件(定义了哪些文件不被Git跟踪)、README文件(提供了项目的基本信息和使用指南)、Gemfile(Rails项目的依赖管理文件)等。 根据以上信息,...
本文档旨在探讨Ruby on Rails中的字符串处理技术。在Ruby语言中,字符串可以通过多种方式创建。具体而言,字符串可以通过单引号('str')或双引号("str")来定义。这两种表示方式的主要区别在于它们对字符串内部...
- 安装或升级此版本前,需要了解其与先前版本的区别,以及可能需要的系统配置要求。 3. **安装与配置**: - 解压缩"redmine 项目管理 v2.0.4 .zip"文件,通常会得到一个包含所有必要文件的目录结构,包括Ruby库、...
这是一个基于模型-视图-控制器模式并模仿 Ruby on Rails 1.2 的框架。 它与 PHP 5.1.4 及更高版本兼容。 可以在以下位置找到文档: : 。 存储库布局 存储库与使用框架构建的... 与 Rails 的一个显着区别是文件以大写
../installdir/ruby/bin/rake db:migrate RAILS_ENV=production ``` #### 四、常见问题及解决方案 在升级过程中可能会遇到一些问题,以下是一些常见的错误及其解决方法: 1. **缺少必要的gem**: - 如果遇到...
在这个背景下,"changes:ruby 版本更改为 2.5、2.6、2.7、3.0 以及大约 3x3" 的标题表明了一个关于Ruby语言不同版本升级的讨论或教程,特别是着重于2.5、2.6、2.7、3.0以及可能的3.x.x(3x3)系列。 首先,让我们...
3. **版本控制**:可能集成了Git或其他版本控制系统,方便团队协作和代码管理,确保代码的版本历史和协同工作流程。 4. **项目管理**:可能包含项目管理工具,支持多项目同时进行,便于组织和跟踪任务,提高工作...
代码注释代码注释是Rails的“ rake notes”功能的node.js版本。 它允许您将注释放入代码中,然后在整个项目中对其进行注释。 代码注释基于两个npm模块,主要是f代码注释代码注释是Rails的“ rake notes”功能的node....
VC++是VC的一个版本,包含对C++语言的支持。需要注意的是,C++是语言,而VC++是使用该语言的开发工具。 VB,全称Visual Basic,是微软开发的基于Basic语言的桌面应用开发工具,主要用于创建C/S系统。然而,随着.NET...
它最初由Knut Urdalen使用Ruby on Rails开发,随着时间的推移逐渐发展成为一个强大的代码管理解决方案。GitLab的主要优势在于其高度的可定制性和灵活性,使得组织可以根据自身的需求定制私有的代码仓库,从而更好地...
此外,它可能还会涉及Ruby的元编程特性,这是Ruby区别于其他语言的一大特色,允许在运行时修改代码,增强了代码的灵活性和动态性。 在Ruby中,类是对象的蓝图,而对象是类的实例。类定义了对象的属性(或称为实例...