Ruby On Rails上手笔记(安装使用全过程)
程序员文章站
2024-01-02 17:48:58
有机会再试一试rails了,只是原来接触的是2,现在已然变成了4,似乎现在的安装比原来会快些。。
rails 4 安装
针对于安装了rvm复制代码 代码如下:gem i...
有机会再试一试rails了,只是原来接触的是2,现在已然变成了4,似乎现在的安装比原来会快些。。
rails 4 安装
针对于安装了rvm
复制代码 代码如下:
gem install rails
没有的话应该这样:
复制代码 代码如下:
sudo gem install rails
安装rvm可以用句
复制代码 代码如下:
curl -l https://get.rvm.io | bash -s stable
查看rails版本
复制代码 代码如下:
rails -v
rails 4.0.3
rails 4.0.3
似乎这就是当前的最新版本
ruby版本
复制代码 代码如下:
we recommend ruby 2.1.0 for use with rails. we stopped supporting ruby 1.8.x after rails 3.2. ruby 1.9.2+ will be supported until rails 5.
官方推荐用2.1.0. 也就是最新版本
安装sqlite
似乎这就是诸如django、rails这类对于轻量级网站的数据库要求。
如果是mac os
复制代码 代码如下:
brew install sqlite3
其他可以看情况安装,如opensuse
复制代码 代码如下:
sudo zypper install sqlite3
rails 4 hello,world
可以直接用rails生成
复制代码 代码如下:
$rails new hello
于是就有了
复制代码 代码如下:
create
create readme.rdoc
create rakefile
create config.ru
create .gitignore
create gemfile
create app
create app/assets/javascripts/application.js
create app/assets/stylesheets/application.css
create app/controllers/application_controller.rb
create app/helpers/application_helper.rb
create app/views/layouts/application.html.erb
create app/assets/images/.keep
create app/mailers/.keep
create app/models/.keep
create app/controllers/concerns/.keep
create app/models/concerns/.keep
create bin
create bin/bundle
create bin/rails
create bin/rake
create config
create config/routes.rb
create config/application.rb
create config/environment.rb
create config/environments
create config/environments/development.rb
create config/environments/production.rb
create config/environments/test.rb
create config/initializers
create config/initializers/backtrace_silencers.rb
create config/initializers/filter_parameter_logging.rb
create config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/secret_token.rb
create config/initializers/session_store.rb
create config/initializers/wrap_parameters.rb
create config/locales
create config/locales/en.yml
create config/boot.rb
create config/database.yml
create db
create db/seeds.rb
create lib
create lib/tasks
create lib/tasks/.keep
create lib/assets
create lib/assets/.keep
create log
create log/.keep
create public
create public/404.html
create public/422.html
create public/500.html
create public/favicon.ico
create public/robots.txt
create test/fixtures
create test/fixtures/.keep
create test/controllers
create test/controllers/.keep
create test/mailers
create test/mailers/.keep
create test/models
create test/models/.keep
create test/helpers
create test/helpers/.keep
create test/integration
create test/integration/.keep
create test/test_helper.rb
create tmp/cache
create tmp/cache/assets
create vendor/assets/javascripts
create vendor/assets/javascripts/.keep
create vendor/assets/stylesheets
create vendor/assets/stylesheets/.keep
run bundle install
接着会安装包依赖
复制代码 代码如下:
fetching gem metadata from https://rubygems.org/..........
fetching additional metadata from https://rubygems.org/..
resolving dependencies...
using rake (10.3.1)
using i18n (0.6.9)
using minitest (4.7.5)
using multi_json (1.9.2)
using thread_safe (0.3.3)
using tzinfo (0.3.39)
using activesupport (4.0.3)
using builder (3.1.4)
using erubis (2.7.0)
using rack (1.5.2)
using rack-test (0.6.2)
using actionpack (4.0.3)
using mime-types (1.25.1)
using polyglot (0.3.4)
using treetop (1.4.15)
using mail (2.5.4)
using actionmailer (4.0.3)
using activemodel (4.0.3)
using activerecord-deprecated_finders (1.0.3)
using arel (4.0.2)
using activerecord (4.0.3)
using bundler (1.5.3)
using coffee-script-source (1.7.0)
using execjs (2.0.2)
using coffee-script (2.2.0)
using thor (0.19.1)
using railties (4.0.3)
using coffee-rails (4.0.1)
using hike (1.2.3)
using jbuilder (1.5.3)
using jquery-rails (3.1.0)
using json (1.8.1)
using tilt (1.4.1)
using sprockets (2.11.0)
using sprockets-rails (2.0.1)
using rails (4.0.3)
using rdoc (4.1.1)
using sass (3.2.19)
using sass-rails (4.0.3)
using sdoc (0.4.0)
using sqlite3 (1.3.9)
using turbolinks (2.2.2)
using uglifier (2.5.0)
your bundle is complete!
use `bundle show [gemname]` to see where a bundled gem is installed.
运行rails
复制代码 代码如下:
$rails server
这样的话打开 http://localhost:3000 就可以看到,rails的欢迎界面welcome aboard,有点类似于django-cms的小马哥~~
创建controller
如官方指南所说http://guides.rubyonrails.org/getting_started.html
执行命令:
复制代码 代码如下:
$rails generate controller welcome index
就会创建下面这些文件:
复制代码 代码如下:
create app/controllers/welcome_controller.rb
route get "welcome/index"
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
create test/helpers/welcome_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/welcome.js.coffee
invoke scss
create app/assets/stylesheets/welcome.css.scss
在config/routes.rb添加默认页面
复制代码 代码如下:
root 'welcome#index'
再运行
复制代码 代码如下:
rails server
就会出现
复制代码 代码如下:
welcome#index
find me in app/views/welcome/index.html.erb
一切来得很快,很突然。