添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

最近在看ruby on rails开发过程中,被其灵活的语法特性以及敏捷的开发风格所深深吸引。将debug信息输出到页面时刻能看到页面参数值对于开发人员进行页面追踪确实方便不少。在适当的页面加入如下代码:

<%= debug(params) if Rails.env.development? %>

推荐在application.html.erb的页脚位置加入,这样可以在每个页面都能够进行追踪。
为了让效果看起来好一点,我们还可以简单进行以下css定义:

@mixin box_sizing {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
/* miscellaneous */
.debug_dump {
  clear: both;
  float: left;
  width: 100%;
  margin-top: 45px;
  @include box_sizing;

将以上代码加入到css.scss中即可。
显示效果如下:
你可以使用File.Open方法创建一个新的文件对象,并分配到一个文件,文件对象。然而,有一个的File.Open和File.new方法之间的差异。所不同的是File.Open方法可以用块,而你不能这样做的同时使用File.new方法.

File.open("filename", "mode") do |aFile|
   # ... process the file
	这里是一个不同的模式打开一个文件列表:
aFile = File.new("/var/www/yiibai.com/ruby/test", "r")
if aFile
   content = aFile.sysread(20)
   puts content
   puts "Unable to open file!"
	这条语句将输出文件的前20个字符。文件指针将被放置在文件中的第21字符.
	syswrite 方法:
你可以使用方法syswrite,写入到一个文件的内容。你需要在写入模式打开文件使用方法syswrite时。例如 :
#!/usr/bin/ruby
aFile = File.new("/var/www/yiibai.com/ruby/test", "r+")
if aFile
   aFile.syswrite("ABCDEF")
   puts "Unable to open file!"
	本声明将写入文件“ABCDEF的”.
	each_byte 方法:
这种方法属于类文件。方法each_byte总是与一个块。请看下面的代码示例::
#!/usr/bin/ruby
aFile = File.new("/var/www/yiibai.com/ruby/test", "r")
if aFile
   aFile.syswrite("ABCDEF")
   aFile.each_byte {|ch| putc ch; putc ?. }
   puts "Unable to open file!"
	字符通过一个个变量ch,然后如下的屏幕上显示:
T.h.i.s. .i.s. .l.i.n.e. .o.n.e.
.T.h.i.s. .i.s. .l.i.n.e. .t.w.o.
.T.h.i.s. .i.s. .l.i.n.e. .t.h.r.e.e.
.A.n.d. .s.o. .o.n.......
	IO.readlines 方法:
类文件的类IO的一个子类。类IO也有一些方法可用于对文件进行操作. IO类的方法之一是IO.readlines。此方法返回行的文件的内容,下面的代码显示使用方法IO.readlines:
#!/usr/bin/ruby
arr = IO.readlines("/var/www/yiibai.com/ruby/test")
puts arr[0]
puts arr[1]
	在这段代码中,变量arr是一个数组。文件测试的每一行,将是一个数组arr的元素。因此,arr[0]将包含首行,而arr[1]将包含文件的第二行.
	IO.foreach 方法:
这种方法也返回逐行输出。该方法之间的差异foreachand readlines方法的方法是该方法的foreach块。然而,不同的方法readlines方法,该方法的foreach不返回一个数组。例如:
#!/usr/bin/ruby
IO.foreach("test"){|block| puts block}
	此代码将通过的文件测试线,由线到可变块的内容,然后输出将显示在屏幕上.
	重命名和删除文件:
你可以重命名和删除文件与红宝石,重命名和删除方法编程。
以下是示例重命名现有的文件test1.txt:
#!/usr/bin/ruby
# Rename a file from test1.txt to test2.txt
File.rename( "test1.txt", "test2.txt" )
	以下是示例删除一个现有的文件test2.txt的:
#!/usr/bin/ruby
# Delete file test2.txt
File.delete("text2.txt")
	文件模式和所有权:
使用掩码的的CHMOD方法改变的模式或文件的权限/访问列表:
以下是示例模式,以改变现有的文件test.txt一个掩码值:
#!/usr/bin/ruby
file = File.new( "test.txt", "w" )
file.chmod( 0755 )
	以下为表中,它可以帮助您选择不同的面具chmod的方法:
File.readable?( "test.txt" )   # => true
File.writable?( "test.txt" )   # => true
File.executable?( "test.txt" ) # => false
	下面的命令查找该文件是否有大小为零或非:
#!/usr/bin/ruby
File.zero?( "test.txt" )      # => true
	以下的命令查找返回文件的大小 :
#!/usr/bin/ruby
File.size?( "text.txt" )     # => 1002
	下面的命令可以用来找出一个文件类型 :
#!/usr/bin/ruby
File::ftype( "test.txt" )     # => file
	ftype方法识别的文件类型返回下列之一:文件,目录,characterSpecial,blockSpecial,fifo,link,socket,或未知.
	下面的命令可以用来发现,当一个文件被创建,修改或上次访问 :
#!/usr/bin/ruby
File::ctime( "test.txt" ) # => Fri May 09 10:06:37 -0700 2008
File::mtime( "text.txt" ) # => Fri May 09 10:44:44 -0700 2008
File::atime( "text.txt" ) # => Fri May 09 10:45:01 -0700 2008
	Ruby 中的目录:
所有文件都包含在不同的目录,Ruby没有处理这些问题。而File类处理文件,目录与Dirclass处理. 通过目录浏览:
要改变在一个Ruby程序的目录,使用如下Dir.chdir。这个例子改变当前目录到/usr/bin中.
Dir.chdir("/usr/bin")
	你可以用Dir.pwd找出哪些当前目录:
puts Dir.pwd # This will return something like /usr/bin
	你可以得到特定使用Dir.entries目录内的文件和目录列表:
puts Dir.entries("/usr/bin").join(' ')
	dir.entries返回一个数组指定的目录内的所有条目。 Dir.foreachprovides的相同的功能:
Dir.foreach("/usr/bin") do |entry|
   puts entry
	一个更简洁的方式是使用目录类数组的方法得到目录列表:
Dir["/usr/bin/*"]
	创建一个目录:
Dir.mkdir可以用来创建目录:
Dir.mkdir("mynewdir")
	您还可以用mkdir设置一个新的目录(不是一个已经存在的权限):
	注: 面具755设置权限,所有者,组,word[anyone] rwxr-XR-X,r=读,w=写,x=执行.
Dir.mkdir( "mynewdir", 755 )
	删除目录:
Dir.delete可以用来删除一个目录。 Dir.unlink和Dir.rmdir执行完全相同的功能,并提供了方便.
Dir.delete("testdir")
	创建文件和临时目录:
临时文件,可能会创建程序的执行过程中短暂的,但不是永久存储的信息。
dir.tmpdir提供对当前系统的临时目录的路径,虽然方法不是默认可用的。到使Dir.tmpdir可用它有必要使用规定“tmpdir”. 可以使用与File.join Dir.tmpdir,创建一个平台独立的临时文件:
require 'tmpdir'
   tempfilename = File.join(Dir.tmpdir, "tingtong")
   tempfile = File.new(tempfilename, "w")
   tempfile.puts "This is a temporary file"
   tempfile.close
   File.delete(tempfilename)
	此代码创建一个临时文件,写入数据,并删除它。 Ruby的标准库还包括称为Tempfile必须是一个库,可以为您创建的临时文件:
require 'tempfile'
   f = Tempfile.new('tingtong')
   f.puts "Hello"
   puts f.path
   f.close
%a – The abbreviated weekday name (“Sun”)
%A – The full weekday name (“Sunday”)
%b – The abbreviated month name (“Jan”)
%B – The full month name (“January”)
%c – The preferred local date and time representation
%d – Day of the month (01..31)
%H – Hour of the day, 24-hour clock (00..23)
%I – Hour of the day, 12-hour clock (01..12)
%j – Day of the year (001..366)
%m – Month of the year (01..12)
%M – Minute of the hour (00..59)
%p – Meridian indicator (“AM” or “PM”)
%S – Second of the minute (00..60)
%U – Week number of the current year,starting with the first Sunday as the firstday of the first week (00..53)
%W – Week number of the current year,starting with the first Monday as the firstday of the first week (00..53)
%w – Day of the week (Sunday is 0, 0..6)
%x – Preferred representation for the date alone, no time
%X – Preferred representation for the time alone, no date
%y – Year without a century (00..99)
%Y – Year with century
%Z – Time zone name
%% – Literal “%” character
一般默认rails bundle install的时候速度会很慢,这是由于tails new的时候会自动去rubygems.org查找是否有更新。但是一般需要的gems早就安装好了,且不用更新,所以上面的步骤就可以省了。强烈建议rails党取消rails new时自动查找gems更新功能!
方法很简单:

rails new myapp --skip-bundle
cd myapp
bundle install --local

另外,最新版bundler速度提高了不少,建议使用,也能提速:

 gem install bundler --pre
第二行会报错:
ActiveModel::ForbiddenAttributesError
Rails.root: /home/gefangshuai/myruby/rails1/blog
Application Trace | Framework Trace | Full Trace app/controllers/comments_controller.rb:7:in `create’ 在@post.comments.new(params[:comment])前面增加:params.permit!
注意要有“!”
修改好的形式如下: @post = Post.find(params[:post_id]) params.permit! #“!”一定要有 @comment = @post.comments.new(params[:comment]) redirect_to @post if @comment.save

checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.
Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby
        --with-mysql-config
        --without-mysql-config
        --with-mysql-dir
        --without-mysql-dir
        --with-mysql-include
        --without-mysql-include=${mysql-dir}/include
        --with-mysql-lib
        --without-mysql-lib=${mysql-dir}/lib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-mlib
        --without-mlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-zlib
        --without-zlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-socketlib
        --without-socketlib
        --with-mysqlclientlib
        --without-mysqlclientlib
        --with-nsllib
        --without-nsllib
        --with-mysqlclientlib
        --without-mysqlclientlib

解决方法:

sudo apt-get install libmysqlclient-dev
gem install mysql