王筝的博客
ruby学习

#这段话可以由下面的话替代
56     respond_to do |format|
57       if @video.save
58         format.html { redirect_to(:action => “index”, :forminfo => @subject) }
59       else
60         format.html { render :action => “new” }
61       end
62     end
63     #用于替代上面的方法
64     if @video.save
65       redirect_to(:action => “index”, :forminfo => @subject)
66     else
67       render :action => “new”
68     end

 

#以下的方法可以由下面的方法替代
102 respond_to do |format|
103 format.html { redirect_to(:action => ‘index’, :forminfo => @subject) }
104 format.xml { head :ok }
105 end
106 #用于替代上面的方法
107 redirect_to(:action => ‘index’, :forminfo => @subject)

查找:

Gsearch -F ‘aa’ -R  –include=*rb

 

替换:

(1)在查找结果中做 :%s/aa/bb/g

(2):Greplace 选择a

(3):wa

$ irb
irb(main):001:0> a = 1
=> 1
irb(main):002:0> if a == 1 || a == 2 || a ==3 ; put ‘a < 4′ ; end
NoMethodError: undefined method `put’ for main:Object
from (irb):2
from /home/wangzheng/.rbenv/versions/1.9.3-p484/bin/irb:12:in `<main>’
irb(main):003:0> if a == 1 || a == 2 || a ==3 ; puts ‘a < 4′ ; end
a < 4
=> nil
irb(main):004:0> [1,2,3,4].include?(1)
=> true
irb(main):005:0> [1,2,3,4].indexOf(1)
NoMethodError: undefined method `indexOf’ for [1, 2, 3, 4]:Array
from (irb):5
from /home/wangzheng/.rbenv/versions/1.9.3-p484/bin/irb:12:in `<main>’
irb(main):006:0> [1,2,3,4].include?(1)
=> true
irb(main):007:0> require ‘active_support/all’
=> true
irb(main):008:0> 1.in? [1,2,3,4]
=> true
irb(main):009:0>

一定要注意,in?后面有一个空格

rails generate controller Users

rails destroy controller Users

在项目的config/routes.rb里面添加resources :user的时候要注意,

rails4.0不能用match,

今天页面跳转都出问题了,各种方法都试过了,log里说语法错误,问了pp,他说是汉字的原因。。。果然。。。

今天需要新建表,以下是建表语句

 

rails generate scaffold users email:string is_admin:integer

此时会生成很多文件,如果之前有相关的文件,系统会询问是否覆盖。

bundle exec rake db:migrate

这时候去数据库里看下,新的表已经生成啦

Could not chdir to home directory /home/wangzheng: No such file or directory
fatal: ‘/gitroot/文件名’ does not appear to be a git repository
fatal: The remote end hung up unexpectedly

其实这是因为没权限导致的错误,哈哈


$bundle exec rails console

irb(main):001:0>CmsAaa.find(111)
irb(main):002:0>
video=CmsAaa.find(111)
video.update_attribute(‘position’,1)
回车
出现true
video.position
回车
出现1