Ruby on Rails 初次冲浪体验
为了更好的阅读体验,欢迎訪问 作者博客原文
Rails is a web application development framework written in the Ruby language. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Experienced Rails developers also report that it makes web application development more fun.
Rails是一个用Ruby编写的Web应用开发框架。
它的设计目标是通过预先提供开发者最開始须要的基础设施。从而让Web应用开发更加easy。
它能够让你写更少的代码来完毕其它语言和框架所不能完毕的工作。有过Rail开发经验的人都说它能让web应用开发变得更有趣。
环境准备
$ ruby -v
ruby 2.2.3p173
$ gem -v
2.4.8
$ sqlite3 --version
3.8.10.2
心里准备
此文档篇幅着实太长,单从文件夹来看就可能被吓退三尺。
实践是最好的学习方式
,你不必太操心,此文章乃纯干货,通篇在编码实践,让你在实践中感受Rails的魅力。它比一个简单的Hello World
更加有趣以及充满了挑战性。
它是一个循序渐进的过程,能让你在每一小步都能获得信心和成就感。同一时候,请相信我。在你到达终点前。你都不必纠结于当中的实现细节。
而当你敲下最后一个回车
的时候,你对Rail的感觉或许会从陌生人到恋人。开启愉快的Rails之旅吧,美好的风景在后面。
创建Rails项目
安装Rails
$ gem install rails
$ rails -v
Rails 4.2.5
创建一个blog应用
$ rails new blog
$ cd blog
生成project的文件夹结构,此处英文较为通俗,建议读一读,心里略微有个数。
File/Folder Purpose app/ Contains the controllers, models, views, helpers, mailers and assets for your application. You’ll focus on this folder for the remainder of this guide. bin/ Contains the rails script that starts your app and can contain other scripts you use to setup, deploy or run your application. config/ Configure your application’s routes, database, and more. This is covered in more detail in 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/ Extended modules for your application. log/ Application log files. public/ The only folder seen by the world as-is. Contains static files and compiled assets. Rakefile This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application. README.rdoc This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on. test/ Unit tests, fixtures, and other test apparatus. These are covered in Testing Rails Applications. tmp/ Temporary files (like cache, pid, and session files). vendor/ A place for all third-party code. In a typical Rails application this includes vendored gems.
Hello, Rails!
启动Rails服务
$ rails server
$ rails server
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-04-06 11:32:26] INFO WEBrick 1.3.1
[2016-04-06 11:32:26] INFO ruby 2.2.3 (2015-08-18) [x86_64-darwin14]
[2016-04-06 11:32:26] INFO WEBrick::HTTPServer#start: pid=18756 port=3000
訪问Rails服务
打开你的浏览器,输入http://localhost:3000。会看到以下页面
加入一个控制器
$ rails generate controller welcome index
$ 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
invoke assets
invoke coffee
create app/assets/javascripts/welcome.coffee
invoke scss
create app/assets/stylesheets/welcome.scss
以下是generator为你生成的两个重要的控制器和视图文件
- app/controllers/welcome_controller.rb
- app/views/welcome/index.html.erb.
将index.html.erb
文件内容替换成:<h1>Hello, Rails!</h1>
- 再次訪问http://localhost:3000,你会有新的发现。
设置应用主页
打开路由配置文件config/routes.rb
,加入内容root 'welcome#index'
:
Rails.application.routes.draw do
root 'welcome#index'
get 'welcome/index'
# The priority is based upon order of creation:
# first created -> highest priority.
#
# You can have the root of your site routed with "root"
# root 'welcome#index'
#
# ...
让程序跑起来
加入Article资源
继续编辑路由配置文件config/routes.rb
。加入内容resources :articles
:
Rails.application.routes.draw do
resources :articles
root 'welcome#index'
end
查看资源的RESTful API
$ rake routes
$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / welcome#index
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
welcome_index GET /welcome/index(.:format) welcome#index
为Article资源加入控制器
$ rails generate controller articles
$ rails generate controller articles
create app/controllers/articles_controller.rb
invoke erb
create app/views/articles
invoke test_unit
create test/controllers/articles_controller_test.rb
invoke helper
create app/helpers/articles_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/articles.coffee
invoke scss
create app/assets/stylesheets/articles.scss
会生成articles_controller.rb
控制器文件
class ArticlesController < ApplicationController
end
创建加入Article的表单视图
创建文件app/views/articles/new.html.erb
,加入表单内容:
<h1>New Article</h1>
<%= form_for :article, url: articles_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
訪问链接http://127.0.0.1:3000/articles/new
为Article加入create控制器方法
打开文件articles_controller.rb
。加入create
方法:
class ArticlesController < ApplicationController
def new
end
def create
render plain: params[:article].inspect
end
end
此时提交表单之后,页面会显示:
{"title"=>"First article!", "text"=>"This is my first article."}`
创建一个Article数据库模型
$ rails generate model Article title:string text:text
为了更好的阅读体验,很多其它内容,欢迎訪问 作者博客原文
Ruby on Rails 初次冲浪体验的更多相关文章
- Ruby on rails初体验(一)
接触ruby on rails 已经有一段时间了,想记录一下自己的rails历程.自己写一些小例子来帮助学习. Rails 适用于那些以数据为中心的应用,很多应用的核心部分包括一个数据库,这些引用的 ...
- 管理不同版本ruby和rails的利器——rvm
近年来,ruby on rails逐渐火了起来,我想各位码农早就耳闻,特别是那些做B/S项目的童鞋,早就想跃跃一试了. 笔者也是初次接触ruby on rails ,我想,对于初学者来说,最好的学习方 ...
- Ruby On Rails环境搭建
注:现在http://rubyforge.org 网站已经停止运行,取而代之的是https://rubygems.org这个网站,下文中所需要的gem包都可以去这个网站搜索下载.其他完全按照下文说的去 ...
- [Ruby on Rails系列]1、开发环境准备:Vmware和Linux的安装
Ruby on Rails是一个采用Ruby语言的遵循MVC模式的Web开发框架.使用RoR会得到更加快速爽快的Web开发体验.相比于Java EE,该框架使Web开发的速度和效率变得更加轻快和敏捷. ...
- Ruby on Rails创始人DHH谈如何进行混合移动APP开发
混合型APP兼具原生型APP软件良好用户交互体验的优势和网页型APP软件跨平台开发的优势,并且其开发成本和网页型APP软件接近,其开发效率也远高于原生型APP软件.混合型APP已经被众多企业所认可.最 ...
- Ruby on Rails (ROR)类书籍
Ruby on Rails (ROR)类书籍下载地址及其他(整理) Ruby on Rails 如此之热,忍不住也去看了看热闹,现在把一些相关的电子图书下载地址整理下,方便有兴趣的朋友. 2006-0 ...
- 开发新手最容易犯的50个 Ruby on Rails 错误(1)
[编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...
- Linux超快速安装Ruby on Rails
Linux超快速安装Ruby on Rails 时间 2014-11-25 11:45:11 Flincllck Talk 原文 http://www.flincllck.com/quick-ins ...
- ruby on rails on windows
这次想系统学会rails,最终目标是将redmine改造成顺手的工具,主要的手段就是开发redmine插件.虽然网上都推荐使用类Unix系统,可手头只有win7系统,就安装了. 难免会遇到这样那样的问 ...
随机推荐
- VUE 内置指令
1.v-if v-else-if v-else <!DOCTYPE html> <html lang="zh"> <head> <meta ...
- Chrome Dev Editor:一个新的JavaScript和Dart IDE
在Google IO 2014期间,一位Google工程师带来了Chrome Dev Editor(CDE).该IDE用于创建面向桌面和移动设备的Chrome应用程序和Web应用程序.CDE支持Jav ...
- 使用Python爬虫爬取网络美女图片
代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...
- ASP.NET MVC3 系列教程 - 模型
I:基础绑定的实现 1.在前面的两篇基础文章(路由 及 控制器&视图)当中,还没对QueryString的绑定进行介绍,因为我觉得它更适合放在这一章节中去介绍.我们在用WebForm去开发的时 ...
- PHP-客户端的IP地址伪造、CDN、反向代理、获取的那些事儿
外界流传的JAVA/PHP服务器端获取客户端IP都是这么取的: 伪代码: 1)ip = request.getHeader("X-FORWARDED-FOR") 可伪造,参 ...
- Linux 网卡丢包严重
http://hi.baidu.com/scstwy/item/cad0fbef1fdc18d3eb34c9d9
- CentOS忘记root密码解决办法
如果是忘记普通的用户密码,那还好说,用root登录命令行界面,修改即可. 但如果是root的话,那就需要这样修改了. 记住,这几篇文章说的都是对的,只是我复杂了,实际只需要将光标移到最后" ...
- php创建对象。真!变!态!
PHP创建类的方式,真是够变态,以下是创建方式: 假设类: class SomeClass {//....} 创建对象: 1.直接通过类名实例化 $obj1 = new SomeClass(); 这种 ...
- Hibernate JPA实体继承的映射(二) @MappedSuperclass
基于代码复用和模型分离的思想,在项目开发中使用JPA的@MappedSuperclass注解将实体类的多个属性分别封装到不同的非实体类中. 1.@MappedSuperclass注解只能标准在类上:@ ...
- cisco asa5510 配置
anyconnect 查看vpn链接 ASA版本8.4(7) anyconnect版本3.1 亲测sh vpn-sessiondb anyconnect 查看登录用户详情sh vpn-ses ...