Ruby on Rails website 的基础是 请求-返回 循环。





首先是浏览器请求服务器,





第二步,Second, in our Rails application, the route takes the request and finds the right Controller and method to handle the request.





第三步,控制器拿到相关数据并发给View模块。





最后,View模块把数据打包好,并且发回给浏览器显示。

-----

搭建开发环境:

  • Creat rails file package: rails new my-app
  • Install Ruby gems
  • Making a controller called pages
  • 在routes.rb文件中,Create a route
  • Create the View

1. Creat rails file package

操作 Rails 用的commands叫做Generator, 在Terminal里,使用如下命令可以新建一个rails app:

rails new my-app

rails就会建立一系列的站点文件。

然后我们在浏览器里输入:localhost:8000就能访问rails建立的第一个web 站点:

Let's learn about the files that you generated.来看看我们建立了哪些文件:

Rails is made up folders that hold our code:

   app - Where our Models, Views and Controllers are. It is where most of our Rails code will go.

   config - Where we find our routes. They handle requests from the browser.

   db - Where we change the state of our database.

   Gemfile - Where our gems are. Gems add special features to our Rails app.



可以参考下图:

2. Install Gem

To run Rails we also need to install Ruby gems. Gems are prepackaged code that make sure our application runs smoothly. They can also add new features.

We add gems through a simple command called bundler. Bundler takes the gems in our Gemfile, an editable file, and stores them in Gemfile.lock, an uneditable file.

To run bundler, we type the following in our terminal:

bundle install

3. Making a controller called pages

In the Request-Response Cycle, we learned that when a user makes a request in the browser, the route handles that request and passes it to the right Controller.

一个route路由来处理浏览器的request,请求。发给Controller

We’ll start building our app by making a Controller called Pages. Controllers decide what information our application needs through methods. Our Pages Controller will be in charge of decisions for the home page of the Etsy app.

How do we generate a Controller?

If we want to generate a Controller called Pages, we enter the following in the terminal:

rails generate controller Pages

Notice that Controller names are always plural.

现在我们就有了pages controller(app/controllers/pages_controller.rb),控制器是通过methods来决策的,这些methods跟前面用的ruby methods是相似的,同时,它们是写在我们创建的Pages Controller里的。



Controller methods主要作用于两方面:

a -- 告诉我们的app,应该显示哪个view

b -- 准备信息,让view可以轻易地显示它。



如果是静态的app,methods就简单了,它只需要告诉Rails哪个view to show.例如:

    def home

    end

这就是告诉controller来render home view. 如果methods里有内容,就会执行它们。这里我们只需要def home 和 end ,Rails就知道是要call the home view.

在app/controllers/pages_controller.rb文件中:

class PagesController < ApplicationController
def home
end
end

操作:

1.

In your Pages Controller, pages_controller.rb, Define a method called def home. You do not need a method body. Hit Run.

2.

In your browser visit localhost:8000/pages/home to see what was created. You can expect to get a Routing Error.

=====================

4. 在routes.rb文件中,Create a route

上一步完成后,我们得到了一个Routing Error的错误,我们前面看到了,来自浏览器的request 是需要一个route进行分发的,分发给controller进行处理。所以我们现在要增加一个routes.rb的文件来补充这个route.



在rails 中,Routes就像app的门一样,来保证正确的分发请求,从而让我们browser 拿到正确的view 返回。



尽管Routes 是我们的第一站,但是我们还是要先拿到controller里的名字和method名字才能写routes。



在routes.rb文件中,we create a route like this, under

Rails.application.routes.draw do:

get 'pages/home'

这里,pages 指的就是controller pages.    home就是指的我们在Controller里定义的那个method的名字home。

在这个事儿里,我们还要创建一个根路径。根路径就是设置一个home page的路径,作为一个app的首页请求。这里, pages/home是我们的首页,所以我们建立的根路径就是:root ’pages#home' (We will also create a root route in this unique
case. A root route is a way to set a route as the home page of the application, or the 'root'. Since pages/home will be our home page, we'll create a root route like this:)

root 'pages#home'

然后在浏览器中再输入 localhost:8000  就能预览效果了。

routes.rb文件源码:

Rails.application.routes.draw do
get 'pages/home'
root 'pages#home'
end

5. Create the View

现在我们已经能够显示一个简单的主页了,不过这个页面只有一句话。我们建立了Controller & Routes, 下一步要Create View了:

Views用来处理用户看到的可见的显示。

Views是怎么来管理presentation的呢?

1.通过html来管理页面。

2.通过ERB - Embedded Ruby ,它是把ruby代码嵌入html页面的一种途径。We can use Ruby objects in our HTML files just as we do in plain Ruby.



由于Views使用了html and ERB,所以View的files总是后缀为.html.erb



在.html.erb文件中,主要有2个categories of ERB: 一个是只在我们代码里显示的code,一种是显示在app里的live code.



如果我们写成:

  <% "goodbye world" %>

    就只有后台能看到。



如果我们写成:

  <%= "hello world" %>

    it will appear live in on our app.就一个=号的区别。



这里这个例子是这样的:

1. 在pages_controller.rb文件中,加入一个method: erb_demo

class PagesController < ApplicationController
def home
end
def erb_demo
end
end

2. 在routes.rb文件中,加入一个route:pages/erb_demo

Rails.application.routes.draw do
get 'pages/home'
root 'pages#home'
get 'pages/erb_demo' #浏览器就可请求localhost:8000/pages/erb_demo,不过,代码里pages/erb_demo是:ControllerName/MethodsName
end

3. 在erb_demo.html.erb文件中,加入两行categories of ERB, 一个不可见,一个可见。

  <%= "This is the erb demo template." %>
<% "goodbye world" %>
<%= "hello world" %>

4. 最后在浏览器中请求: localhost:8000/pages/erb_demo    ,可以得到:

至此,一个ruby环境的搭建,建立一个简单的ruby app来显示一个网页,就搞定了。

Ruby学习笔记2 : 一个简单的Ruby网站,搭建ruby环境的更多相关文章

  1. blfs(systemv版本)学习笔记-制作一个简单的桌面系统

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! 大概思路: lfs(系统)+xorg(驱动)+i3-wm(窗口+桌面)+lightdm(显示管理器+登录管理器) 链接: lfs ...

  2. Oracle学习笔记:一个简单的行转列例子

    一个简单的行列转换例子,原始数据. create table temp_cwh_student ( name ), subject ), score ) ) select * from temp_cw ...

  3. cpp学习笔记 1一个简单的小程序以及一些的知识点

    今天买的cpp到了从今天開始又一次学习cpp如今发现学校发的书真的不怎莫样. <em>#include<stdio.h>//预处理命令 int main()/*第一个被调用的函 ...

  4. Spring学习笔记--声明一个简单的Bean

    spring依赖的maven dependencyhttp://mvnrepository.com/artifact/org.springframework 在pom.xml中添加如下依赖: < ...

  5. Windows程序设计学习笔记(1):一个简单的windows程序

    <Windows程序设计>(第五版)(美Charles Petzold著) #include<windows.h> LRESULT CALLBACK WndProc(HWND, ...

  6. ROS学习笔记11-写一个简单的服务和客户端(C++版本)

    本文主要来源于:http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28c%2B%2B%29 写一个服务节点.在创建消息和服务中,我们创建了一 ...

  7. ROS学习笔记10-写一个简单的订阅者和发布者(C++版本)

    本文档来源于:http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29 写发布者节点如前所述,节点是连接到RO ...

  8. Ruby学习笔记4: 动态web app的建立

    Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...

  9. ruby学习笔记(1)-puts,p,print的区别

    ruby学习笔记-puts,p,print的区别 共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将 ...

  10. ASP.NET MVC Web API 学习笔记---第一个Web API程序

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...

随机推荐

  1. 一些常用的排序算法(C版)

    1. 直接插入排序(稳定排序) 简单的说就是将序列分为有序序列和无序序列.每一趟排序都是将无序序列的第一个元素插入有序序列中.R[1… i-1] <- R[i…n] , 每次取R[i]插入到R[ ...

  2. position实现分层和遮罩层功能

    很多网站,当点了一个按钮后,弹出一个窗口,底层变透明不可选,就是用到层的概念,至少三层 第一层,底层原始层 第二层,遮罩层,用到positon: fixed; top bottom left righ ...

  3. MIME 设置

    1,打开iis7,选择你要设置网站,打开mime类型选项 2,找到.rar的mime类型,复制他的类型 3,复制后选项添加,在文件扩展名那一栏填入.*,然后在下面的mime类型复制你刚复制的appli ...

  4. msp430及stm32中基本的C编程知识

    为什么我使用P1OUT ^= 0x01;和P1OUT = 0x01 ^是异或计算符号 所以 每次运算都是反转的.而不不加这个运算符就是一直保持1的状态. p1out|=bit6的意思p1out的值如果 ...

  5. <亲测>ubuntu 16.04 忘记root密码

    ubuntu 16.04 忘记root密码   阅读目录 方法一 方法二 虚拟机中安装的ubuntu 16.04. 回到目录 方法一 如果用户具有sudo权限,那么直接可以运行如下命令: sudo s ...

  6. SpringBoot之退出服务(exit)时调用自定义的销毁方法

    我们在工作中有时候可能会遇到这样场景,需要在退出容器的时候执行某些操作.SpringBoot中有两种方法可以供我们来选择(其实就是spring中我们常用的方式.只是destory-method是在XM ...

  7. bzoj5011: [Jx2017]颜色

    Description 可怜有一个长度为n的正整数序列Ai,其中相同的正整数代表着相同的颜色. 现在可怜觉得这个序列太长了,于是她决定选择一些颜色把这些颜色的所有位置都删去. 删除颜色i可以定义为把所 ...

  8. Dubbo(2)发布Dubbo服务

    主要参考Dubbo源码包里面的dubbo-demo源码: 1.项目结构: 2.pom.xml中的依赖: <project xmlns="http://maven.apache.org/ ...

  9. Java学习——this、this()、super 和 super()的使用

    编写程序:说明 this.super 和 super()的用法.程序首先定义 Point(点)类,然后创建点的子类 Line(线)),最后通过 LX7_3 类输出线段的长度. package Pack ...

  10. etcd 命令行(转)

    原文 https://www.cnblogs.com/breg/p/5756558.html 比较重要的配置 -name 节点名称,默认是UUID-data-dir 保存日志和快照的目录,默认为当前工 ...