Tagged: Rails RSS

  • moonfox 1:45 am on July 9, 2010 Permalink | Log in to leave a Comment
    Tags: Rails   

    rake db:migrate CREATE TABLE `schema_migrations` 

    在使用rails 2.3.8与mysql 5.1 开发时,运行rake db:migrate 出现CREATE TABLE `schema_migrations` (`version`varchar(255) NOT NULL) ENGINE=InnoDB错误提示,原因是新版mysql的客户端连接库对rails 2.3的支持不够好导致的。

    解决方法,换一个旧版本的mysql的libmySQL.dll文件,复制到ruby目录下的bin文件夹中就可以解决这个问题了。

    附libmySQL.dll下载地址:http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll

     
  • moonfox 12:24 am on May 26, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    rails项目开发环境一定要与服务器生产环境一致 

    在使用ruby1.8.6时,发现字段串也有.last方法,即取出字符串的最后一位,后来在ruby1.8.7的环境下调用此代码,被告之没有定义last方法,查询api实确没有找到last方法,开发环境下使用的是ruby1.8.6版本,而ruby1.8.7是生产环境下的版本,还好发现的早,错误及时在发生前被终止。

    由于开发环境在windows下,所以使用的ruby版本为1.8.6二进制发行版,而服务器为linux环境,ruby版本为1.8.7,从而导致出现上述不兼容问题,所以开发环境务必要与生产环境一致,否则你写的每一行代码都将会成为一串串定时炸弹,不定在什么时候就会炸开, 所以为了我们项目的安全,我们在开发时的环境必须与服务器生产环境一致。

     
  • moonfox 9:28 pm on May 23, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    ruby 安装 rmagick 

    sudo apt-get install libmagick9-dev ruby1.8-dev
    sudo apt-get install imagemagick
    sudo gem install rmagick

     
  • moonfox 6:21 pm on May 5, 2010 Permalink | Log in to leave a Comment
    Tags: , Rails,   

    mysql 字符串转换成数字 

    在mysql中使用cast 函数将数字转换字符时,发现这样一个现象,将字符串“6,666”转换为数字时,预期值应该为6666,但实际值为6,并且rails的1.22版本也存在同样的问题。

    解决方法,很无奈,将“6,666”中的”,”去掉,再进行转换。

     
  • moonfox 12:30 am on May 4, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    rails redirect_to 

    做了一个出错后跳转的页面,运行后发现程序进入死循环,查看日志发现页面被回指到了自己,原因是在redirect_to的时候自动继承了链接中的参数,由此得出结论,在带参数的情况下将页面回指到自己时,如果只指定了action,链接的参数会被自动继承,若要去掉参数,需要在redirect_to的时候重新定义。

     
  • moonfox 11:34 pm on March 1, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    ruby 1.8.7 and rails 1.2.2完美匹配 

    由于ruby高版本的ruby与低版本的rails存在着兼容性问题,导致在不同的版本之间都有着这样或那样的问题,如(ruby1.8.7与rails 1.2.2就存在mysql接口调用问题),经过一番试验,发现ruby 1.8.7 (2009-06-12 patchlevel 174)与rails1.2.2有着良好的兼容性,目前还没有发现任何问题!

     
  • moonfox 6:33 pm on February 17, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    image_tag(“icon”) # =>
    Icon
    image_tag(“icon.png”) # =>
    Icon
    image_tag(“icon.png”, :size => “16×10″, :alt => “Edit Entry”) # =>
    Edit Entry
    image_tag(“/icons/icon.gif”, :size => “16×16″) # =>
    Icon
    image_tag(“/icons/icon.gif”, :height => ’32′, :width => ’32′) # =>
    Icon
    image_tag(“/icons/icon.gif”, :class => “menu_icon”) # =>
    Icon
    image_tag(“mouse.png”, :mouseover => “/images/mouse_over.png”) # =>
    Mouse
    image_tag(“mouse.png”, :mouseover => image_path(“mouse_over.png”)) # =>
    Mouse

     
  • moonfox 10:32 pm on February 15, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    render partial 

    # Renders the same partial with a local variable.
    render :partial => “person”, :locals => { :name => “david” }

     
  • moonfox 11:01 pm on February 14, 2010 Permalink | Log in to leave a Comment
    Tags: Rails,   

    validates_inclusion_of 

    class Person < ActiveRecord::Base
    validates_inclusion_of :gender, :in => %w( m f )
    validates_inclusion_of :age, :in => 0..99
    validates_inclusion_of :format, :in => %w( jpg gif png ), :message => “extension {{value}} is not included in the list”
    end

     
  • moonfox 2:48 pm on December 7, 2009 Permalink | Log in to leave a Comment
    Tags: Rails   

    rails2.0快速体验
    1、新建一个Rails程序
    rails todo
    这时使用的是默认的sqlite3做数据库。如果你希望使用mysql,则输入
    rails todo -d mysql

    2、修改数据库配置文件
    cd todo
    进入项目所在文件夹,找config目录下的database.yml 数据库配置文件,修改数据库用户名与密码配置
    development:
    adapter: mysql
    encoding: utf8
    database: todo_development
    username: root
    password: root

    test:
    adapter: mysql
    encoding: utf8
    database: todo_test
    username: root
    password: root

    production:
    adapter: mysql
    encoding: utf8
    database: todo_production
    username: root
    password: root
    注意:“:”与后面的字符串之间要有一个空格

    3、创建数据库
    rake db:create:all
    这里又一个新东西“rake db:create:all”,它将给你建立起各个数据库,现在不需要你自己去手工搞了。是不是比以前爽了。
    运行后如果什么反应都没有,则说明数据库创建成功。

    4、rails的魔法就在这里开始。
    用脚手架生成一个关于todo的应用
    ruby script/generate scaffold todo title:string body:text done:boolean due:datetime
    运行后会发现生成许多文件,rails帮助我们做了许多事件,它生成了todo这个应用的model,view,controller,这不就是MVC吗,已经帮助你生成了最基本的框架,下面你要做的事情就是不断向这个框架里填充东西。

    脚手架又为我们生成了一个创建表的migration(迁移任务),让我们再次运行迁移任务
    rake db:migrate

    提示:当你已经熟练掌握rails的开发时,就可以不用脚手架了。

    5、让我们使用下rails的魔法吧
    启动我们的项目
    ruby script/server
    然后用浏览器访问下面的链接http://127.0.0.1:3000/todos
    搞定了一个todolist

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
esc
cancel