ruby on rails 常用的记录

Rails教程:路由与测试
本文介绍了Rails应用中的路由配置方法及如何进行测试。详细解释了如何为静态页面创建具名路由,包括根路由和其他常见页面如帮助和支持页面。此外还提到了如何使用HTTP GET请求映射到控制器的动作。

撤销操作:
https://railstutorial-china.org/book/chapter3.html#aside-undoing-things

HTTP 超文本传输协议
https://railstutorial-china.org/book/chapter3.html#aside-get-etc

测试
https://railstutorial-china.org/book/chapter3.html#aside-when-to-test
https://railstutorial-china.org/book/chapter5.html#layout-link-tests


路由

 # 对根路由来说,创建的具名路由是 root_path 和 root_url。二者之间唯一的区别是,后者是完整的 URL:
 root_path -> '/'
 root_url  -> 'http://www.example.com/'

 # 本书遵守一个约定:只有重定向使用 _url 形式,其余都使用 _path 形式。
 # 把发给 /help 的 GET 请求交给 StaticPages 控制器中的 help 动作处理。
 # 与根路由一样,这个规则也会定义两个具名路由,分别是 help_path 和 help_url:
 help_path -> '/help'
 help_url  -> 'http://www.example.com/help'
 # 定义路由
 # config/routes.rb

 Rails.application.routes.draw do
  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
 end

 # 视图中使用
 <%= link_to "About", about_path %>

创建控制器、视图、测试、帮助函数[controller、view、help、test]文件

rails generate controller Users new

创建 模型 model

rails generate model User name:string email:string

视图中使用 ruby

# 嵌入式 Ruby 代码
# 设置参数
<% provide(:title, "The Title") %>
# 调用参数
<%= yield(:title) %>

定义辅助方法
app/helpers/application_helper.rb

module ApplicationHelper

  # 根据所在的页面返回完整的标题
  def full_title(page_title = '')
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      page_title + " | " + base_title
    end
  end

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值