当前位置: 首页 > news >正文

Ruby On Rails 笔记1——Rails 入门

突然想跟着官方文档把Ruby On Rails过一遍,把一些有用的记下来就可以一直看了,do它! https://guides.rubyonrails.org/v7.2/

注:官网是英文文档,我自己翻译了一下,不确保完全准确,只供自己学习开发使用。

Rails is a web application framework running on the Ruby programming language. There are several curated lists of online resources for learning Ruby:

  • Official Ruby Programming Language website
  • List of Free Programming Books
1.Rails 是什么?

Rails是一个由Ruby编程语言编写的Web应用开发框架。它是为了让编写Web应用更简单而设计

2. Say "Hello", Rails

在完成安装启动后,想开始写代码了,you need to create at minimum a route, a controller with an action, and a view. A route将请求映射到controller action; A controller action里有处理请求的工作,以及给View准备的数据。A view根据需要的格式显示数据。

Step1 在routes文件里添加一个route

Rails.application.routes.draw doget "/articles", to: "articles#index"# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

这个route声明将GET /articles请求映射到ArticlesController 的index action.

Step2 通过下面这个命令生成ArticlesController和index

bin/rails generate controller Articles index --skip-routes

生成的文件长这样app/controllers/articles_controller.rb

class ArticlesController < ApplicationControllerdef indexend
end

默认情况下,Rails会自动render与controller 和 action名字相同的view,即app/views/articles/index.html.erb

Step3 在index.html.erb文件里写上

<h1>Hello, Rails!</h1>

接下来启动server访问http://localhost:3000/articles就可以了。

也可以通过添加route

root "articles#index"

将其设置成home page, 即访问http://localhost:3000就能打开index.html.erb文件.

3. 生成一个Model

上述提到routes, controllers, actions, and views,这些都是遵循MVC (Model-View-Controller)模式的Web应用典型组成部分。我们已经拥有了Controller和View,继续生成一个Model吧。

model is a Ruby class that is used to represent data. Additionally, models can interact with the application's database through a feature of Rails called Active Record.

英文原文太好了,不翻译了。可以跟官网一样用model generator也可以自己创建app/models/article.rb。

可以根据数据库里需要存的字段定义这个model

至此最基本的概念和架构已经清楚了。

http://www.lryc.cn/news/497537.html

相关文章:

  • 高效开发 Python Web 应用:FastAPI 数据验证与响应体设计
  • 基于“开源 2+1 链动 O2O 商城小程序”的门店拉新策略与流程设计
  • 33.5 remote实战项目之设计prometheus数据源的结构
  • 微服务springboot详细解析(一)
  • 深入探讨Go语言中的双向链表
  • Fastapi + vue3 自动化测试平台---移动端App自动化篇
  • ElasticSearch easy-es 聚合函数 group by 混合写法求Top N 词云 分词
  • 在 ASP.NET C# Web API 中实现 Serilog 以增强请求和响应的日志记录
  • 2024年顶级小型语言模型前15名
  • 精通 Python 网络安全(一)
  • 【python自动化二】pytest集成allure生成测试报告
  • 网络版本的通讯录青春版(protobuf)
  • 开源模型应用落地-安全合规篇-用户输入价值观判断(三)
  • 神经网络入门实战:(十四)pytorch 官网内置的 CIFAR10 数据集,及其网络模型
  • 【Rust在WASM中实现pdf文件的生成】
  • 在MySQL中执行sum case when报错:SUM does not exist
  • 【openssl】相关指令
  • 实例分割详解
  • D87【python 接口自动化学习】- pytest基础用法
  • 浅谈MySQL路由
  • matlab中disp,fprintf,sprintf,display,dlmwrite输出函数之间的区别
  • 30.100ASK_T113-PRO 用QT编写视频播放器(一)
  • Linux-GPIO应用编程
  • opencvocr识别手机摄像头拍摄的指定区域文字,文字符合规则就语音报警
  • 微服务即时通讯系统(5)用户管理子服务,网关子服务
  • postgreSQL安装后启动有The application server could not be contacted问题
  • 架构05-架构安全性
  • 虚幻引擎---材质篇
  • NPM镜像详解
  • 从智能合约到去中心化AI:Web3的技术蓝图