学习ruby on rails

入门文档

http://guides.ruby-china.org/getting_started.html

install

ruby-lang & sqlite

1
2
3
4
5
6
7
8
9
10
11
yum install ruby-devel sqlite sqlite-devel ruby-rdoc

ruby --version
# show ############
# ruby 2.0.0p598 (2014-11-13) [x86_64-linux]
##############

sqlite3 --version
# show ############
# 3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668
##############

gem

换源

需要换源,原因

由于国内网络原因(你懂的),导致 rubygems.org 存放在 Amazon S3 上面的资源文件间歇性连接失败。所以你会与遇到 gem install rack 或 bundle install 的时候半天没有响应,具体可以用 gem install rails -V 来查看执行过程。

以上引用自https://ruby.taobao.org/,好人一生平安。

方法

1
2
3
4
5
6
7
gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
gem sources -l
# show ############
# *** CURRENT SOURCES ***
#
# https://ruby.taobao.org/
##############

说明换成了taobao的源

1
bundle config mirror.https://rubygems.org https://ruby.taobao.org

bundle也需要换,然而似乎还是没啥用,后面还是得改Gemfile

rails

1
gem install rails

漫长的等待,就装好了,查看一下版本吧

1
2
3
4
rails --version
# show ############
# Rails 4.2.4
##############

new project

1
rails new learn_ror

卡在bundle install,原因是learn_ror/Gemfile里的source指向的还是官网,所以需要手动改成淘宝的源

1
2
# source 'https://rubygems.org'                                                                                                                                                                             
source 'https://ruby.taobao.org'

再次执行install

1
2
cd learn_ror
bundle install

启动

执行启动命令

1
rails server

提示缺少jsruntime,看了下https://github.com/rails/execjs,决定采用therubyracer,在Gemfile里添加一行

1
https://github.com/cowboyd/therubyracer

再次bundle install,rails server,欢迎页面就出现了。

如果指定端口-p,则提示create_listener没权限

1
2
3
4
5
6
7
8
9
10
11
12
[2015-10-14 10:22:46] INFO  WEBrick 1.3.1
[2015-10-14 10:22:46] INFO ruby 2.0.0 (2014-11-13) [x86_64-linux]
[2015-10-14 10:22:46] WARN TCPServer Error: Permission denied - bind(2)
Exiting
/home/g640/.gem/ruby/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- abrt/handler (LoadError)
from /home/g640/.gem/ruby/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
from /home/g640/.gem/ruby/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/g640/.gem/ruby/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
from /home/g640/.gem/ruby/gems/abrt-0.1.1/lib/abrt.rb:6:in `block in <top (required)>'
/usr/share/ruby/webrick/utils.rb:85:in `initialize': Permission denied - bind(2) (Errno::EACCES)
from /usr/share/ruby/webrick/utils.rb:85:in `new'
from /usr/share/ruby/webrick/utils.rb:85:in `block in create_listeners'

这个时候需要

1
2
sudo semanage port -a -t http_port_t -p tcp 3000
sudo iptables -I INPUT -p TCP --dport 3000 -j ACCEPT

hello world

接下来就是照着教程做了,没啥技术含量。。。。。。。
练习的代码放在https://github.com/CodeJuan/learn_ror


本博客欢迎转发,但请保留原作者信息
github:codejuan
博客地址:http://blog.decbug.com/