您的位置:首页 > 编程语言 > Ruby

Ruby on Rails Guides(一)

2015-09-03 20:16 435 查看

准备工作

安装Ruby1.9.3或者更新

sudo apt-get install ruby-full


安装RubyGems

下载https://rubygems.org/pages/download#formats

解压

3.安装

ruby setup.rb


安装SQLite

下载 https://www.sqlite.org/download.html

安装 Sqlite

下载源码进行编译

./configure
make
sudo make install


安装Rails

gem install rails


安装的时候出现了错误,Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org

是GFW的原因,重新配置源

gem sources --remove https://rubygems.org/ gem sources -a https://ruby.taobao.org/ gem sources -l
*** CURRENT SOURCES *** https://ruby.taobao.org # 请确保只有 ruby.taobao.org
gem install rails


创建第一个Blog程序

rails new blog


问题:一直卡在run bundle install

原因:运行这个的时候,会用默认的源。有两种方式:

rails new blog –skip-bundle 或者在run bundle install时按Ctrl-C终止;然后修改Gemfile里面的第一行,将source ‘https://rubygems.org’ 改为 source ‘http://ruby.taobao.org

改完source后,还需要运行bundle install

自动生成的目录说明:

├── app
│   ├── assets
│   │   ├── images
│   │   ├── javascripts
│   │   │   └── application.js
│   │   └── stylesheets
│   │       └── application.css
│   ├── controllers
│   │   ├── application_controller.rb
│   │   └── concerns
│   ├── helpers
│   │   └── application_helper.rb
│   ├── mailers
│   ├── models
│   │   └── concerns
│   └── views
│       └── layouts
│           └── application.html.erb
├── bin
│   ├── bundle
│   ├── rails
│   ├── rake
│   └── setup
├── config
│   ├── application.rb
│   ├── boot.rb
│   ├── database.yml
│   ├── environment.rb
│   ├── environments
│   │   ├── development.rb
│   │   ├── production.rb
│   │   └── test.rb
│   ├── initializers
│   │   ├── assets.rb
│   │   ├── backtrace_silencers.rb
│   │   ├── cookies_serializer.rb
│   │   ├── filter_parameter_logging.rb
│   │   ├── inflections.rb
│   │   ├── mime_types.rb
│   │   ├── session_store.rb
│   │   └── wrap_parameters.rb
│   ├── locales
│   │   └── en.yml
│   ├── routes.rb
│   └── secrets.yml
├── config.ru
├── db
│   └── seeds.rb
├── Gemfile
├── Gemfile.lock
├── lib
│   ├── assets
│   └── tasks
├── log
├── public
│   ├── 404.html
│   ├── 422.html
│   ├── 500.html
│   ├── favicon.ico
│   └── robots.txt
├── Rakefile
├── README.rdoc
├── test
│   ├── controllers
│   ├── fixtures
│   ├── helpers
│   ├── integration
│   ├── mailers
│   ├── models
│   └── test_helper.rb
├── tmp
│   └── cache
│       └── assets
└── vendor
└── assets
├── javascripts
└── stylesheets


app/ 包括了 controllers, models, views, helpers, mailers and assets ,主要精力应该放在这个目录下。

bin/ 包括了启动app的脚本,也可以包括其他用于setup,deploy和run的脚本。

config/ 配置程序的 routes, database等等. 详细参照 Configuring Rails Applications.

config.ru Rack configuration for Rack based servers used to start the application.

db/ Contains your current database schema, as well as the database migrations.

Gemfile

Gemfile.lock These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the Bundler website.

lib/ 为你的程序扩展modules。

log/ 程序的log文件。

public/ 唯一的可以被外部看到的文件,包括了一些静态的文件和编译过的文件。

Rakefile 这个文件用于保存可以命令行运行的task。这些task被整个Rails组件定义。你应该增加自己的任务,通过添加新的文件到lib/tasks下面,而不是改变Rakefile。

README.rdoc 这是程序的简洁说明指南,你应该编辑这个文件告诉其他人你的程序是做什么的,怎么设置等等。

test/ Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications.

tmp/ 临时文件 (像 cache, pid, session files).

vendor/ 这个目录是放第三方的代码,一个标准的Rails程序,包括了vendored gems。

设置首页

访问 http://localhost:3000/welcome/index,可以看到刚才创建的View,但是如果希望把他改成首页。

编辑config/routes.rb

Rails.application.routes.draw do
get 'welcome/index'

# The priority is based upon order of creation:
# first created -> highest priority.
# See how all your routes lay out with "rake routes".
#
# You can have the root of your site routed with "root"
# root 'welcome#index'
#
# ...


将其中的root ‘welcome#index’去掉注释。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rails ruby 安装