Ruby on RailsRuby、Rails知识Rails 成功之路

Ruby on Rails开发基础

2017-03-22  本文已影响60人  廖马儿

简单介绍

  1. 路由:

    Rails.application.routes.draw do
     root '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'
     #
     # ...
    

root 'welcome#index' 告知 Rails,访问程序的根路径时,交给 welcome 控制器中的 index 动作处理。get 'welcome/index' 告知 Rails,访问 http://localhost:3000/welcome/index 时,交给 welcome 控制器中的 index 动作处理。get 'welcome/index' 是运行 rails generate controller welcome index 时生成的。

2.资源

资源可以被创建、读取、更新和删除,这些操作简称 CRUD。
Rails 提供了一个 resources 方法,可以声明一个符合 REST 架构的资源。

开始学习

使用广泛,发展迅猛!
对于web应用来书,REST是最好的
约定大于配置
不要重复自己

第一部分

Rails框架总览

推荐的IDE:
RubyMine:比较大
Vim:支持的插件很丰富,配置复杂

新建一个rails工程:

Rails new projName --skip-bundle #工程名为projName  --skip-bundle 避免卡住

Gemfile中: source 'https://rubygems.org' 这个是官方的gem源,国内访问很慢。
如果创建工程的时候,不skip,那么会去这个源下载.

ver
`vendor` :存放三方库
`Gemfile`:配置了这个工程的所有gem包

rails3之后引入了bundle,来管理包。
bundle相当于iOS 的cocoapods

MVC设计思想

为什么要介绍MVC?

MVC设计模式的概念

Model:模型
View:视图
Controller:控制器

控制器中的action

action是控制器的基本组成单元。
控制器不处理请求,action去处理请求。

class WelcomeController < ApplicationController

ApplicationController就是rails中的控制器的基类 < 继承于
里面就有预先定义好的很多方法

在Terminal中可以使用destory删除我们生成的
generate相对应

config文件夹:
很重要
routes.rb:显示了所有路由配置文件信息

root 'welcome#index'

配置了,访问根目录时候,向welcome控制器的index action发送请求。

资源

resources
十分重要

REST概念

REST以及RESTful的相关概念
REST就是用于描述用户client和服务server的交互的一个东西。交互方式。请求方式。

REST:资源的表现层状态转换。REST服务是一种无状态的服务。定位资源。
资源:只要你能够获取的信息都叫资源。
表现层:
状态转换:建立在表现层上面的。

使用URL定位资源

需要给出一个确定的,能够获取确定信息的Url

使用HTTP动作(GET POST DELETE)描述操作

服务器如果保存状态的话,服务器的压力会很大,用户越多压力越大。

RESTful可以跨平台使用,

一种资源路由,只能生成7中不同的路由访问。

路由

路由并不是只有转发请求这么一个功能。
Rails路由的基本功能:

  1. 接受并识别HTTP请求
  2. 处理URL附加参数
  3. 识别link_to和redirect_to

路由应该怎么配置怎么使用呢?

路由的种类:

  1. 一般路由
  2. 命名路由
  3. 资源路由
Rails.application.routes.draw do

   resources 'posts' # 这个是配置了7种路由方式

如果:

resources 'post', :except => :show

排除掉show这个路由

自定义路由:

post 'posts/:id', :to => 'posts#show'

在rails中配置一般路由

集合路由:posts/recent

resources 'posts' do
    #get 'recent', :on => :collection # 集合路由扩展
    collection do
      get 'recent'
  
    end
  end

成员路由:posts/:id/recent

一般使用集合路由和成员路由来添加资源路由扩展。

路由&Action Controller

展示所有的action routes:

$ rake routes

这个是action 中定义变量,在view中使用:

class PostsController < ApplicationController
  def index # define a action
    @content_first = 'This is some sample text for our awesome new Ruby blog';
    @content_second = 'This is more sample text for our awesome new Ruby blog';

  end

使用:

<h1>This is posts index</h1>
<h3>Sample post one</h3>
<p><%= @content_first %></p>
<hr>

<h3>Sample post two</h3>
<p><%= @content_second %></p>

注意:<%= @content_first %> 需要有@

ruby on rails 数据交互

一, 第一部分
ActiveRecord

创建model

命令行:

rails g model category  name:string # g == generate

heroku 教程
https://devcenter.heroku.com/articles/getting-started-with-ruby#deploy-the-app

创建脚手架

$ rails g scaffold Movie title:string description:text movie_length:string director:string rating:string

上一篇下一篇

猜你喜欢

热点阅读