Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
f207e00e
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
f207e00e
编写于
1月 27, 2020
作者:
E
Eileen M. Uchitelle
提交者:
GitHub
1月 27, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #38268 from radar/getting-started-guide-improvements
Revise Getting Started Guide
上级
a6841c72
1c9631ac
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
66 addition
and
20 deletion
+66
-20
guides/source/getting_started.md
guides/source/getting_started.md
+66
-20
未找到文件。
guides/source/getting_started.md
浏览文件 @
f207e00e
...
...
@@ -91,7 +91,7 @@ ruby 2.5.0
```
Rails requires Ruby version 2.5.0 or later. If the version number returned is
less than that number, you'll need to install a fresh copy of Ruby.
less than that number
(such as 2.3.7, or 1.8.7)
, you'll need to install a fresh copy of Ruby.
TIP: To quickly install Ruby and Ruby on Rails on your system in Windows, you can use
[
Rails Installer
](
http://railsinstaller.org
)
. For more installation methods for most
...
...
@@ -232,8 +232,7 @@ enough to serve a page.
### Say "Hello", Rails
To get Rails saying "Hello", you need to create at minimum a _controller_ and a
_view_
.
To get Rails saying "Hello", you need to create at minimum a _route_, a _controller_ and a _view_.
A controller's purpose is to receive specific requests for the application.
_Routing_
decides which controller receives which requests. Often, there is more
...
...
@@ -242,43 +241,87 @@ different _actions_. Each action's purpose is to collect information to provide
it to a view.
A view's purpose is to display this information in a human readable format. An
important distinction to make is that
it is
the _controller_, not the view,
where information is collected. The view should just display that information.
important distinction to make is that the _controller_, not the view,
is
where information is collected. The view should just display that information.
By default, view templates are written in a language called eRuby (Embedded
Ruby) which is processed by the request cycle in Rails before being sent to the
user.
When we make a request to our Rails application, we do so by making a request
to a particular _route_. To start off, let's create a route in
`config/routes.rb`
:
```
ruby
Rails
.
application
.
routes
.
draw
do
get
"/articles"
,
to:
"articles#index"
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
```
This is your application's _routing file_ which holds entries in a special
[
DSL
(domain-specific
language)
](
https://en.wikipedia.org/wiki/Domain-specific_language
)
that tells
Rails how to connect incoming requests to controllers and actions.
The line that we have just added says that we are going to match a
`GET
/articles`
request to
`articles#index`
. This string passed as the
`to`
option
represents the _controller_ and _action_ that will be responsible for handling
this request.
Controllers are classes that group together common methods for handling a
particular _resource_. The methods inside controllers are given the name
"actions", as they _act upon_ requests as they come in.
To create a new controller, you will need to run the "controller" generator and
tell it you want a controller called "
Welcome
" with an action called "index",
tell it you want a controller called "
articles
" with an action called "index",
just like this:
```
bash
$
bin/rails generate controller
Welcome
index
$
bin/rails generate controller
articles
index
```
Rails will create several files and a route for you.
```
bash
create app/controllers/
welcome
_controller.rb
route get
'welcome
/index'
create app/controllers/
articles
_controller.rb
route get
'articles
/index'
invoke erb
create app/views/
welcome
create app/views/
welcome
/index.html.erb
create app/views/
articles
create app/views/
articles
/index.html.erb
invoke test_unit
create
test
/controllers/
welcome
_controller_test.rb
create
test
/controllers/
articles
_controller_test.rb
invoke helper
create app/helpers/
welcome
_helper.rb
create app/helpers/
articles
_helper.rb
invoke test_unit
invoke assets
invoke scss
create app/assets/stylesheets/
welcome
.scss
create app/assets/stylesheets/
articles
.scss
```
Most important of these are of course the controller, located at
`app/controllers/welcome_controller.rb`
and the view, located at
`app/views/welcome/index.html.erb`
.
Most important of these is the controller, located at
`app/controllers/articles_controller.rb`
.
Open the
`app/views/welcome/index.html.erb`
file in your text editor. Delete all
Let's look at that controller now:
```
ruby
class
ArticlesController
<
ApplicationController
def
index
end
end
```
This controller defines a single action, or "method" in common Ruby terms,
called
`index`
. This action is where we would define any logic that we would
want to happen when a request comes in to this action. Right at this moment, we
don't want this action to do anything, and so we'll keep it blank for now.
When an action is left blank like this, Rails will default to rendering a view
that matches the name of the controller and the name of the action. Views in a
Rails application live in
`app/views`
, and so the default view for this action
is going to be
`app/views/articles/index.html.erb`
.
Open the
`app/views/articles/index.html.erb`
file in your text editor. Delete all
of the existing code in the file, and replace it with the following single line
of code:
...
...
@@ -286,6 +329,9 @@ of code:
<h1>
Hello, Rails!
</h1>
```
If we go back to our browser and make a request to
<http://localhost:3000/articles>
, we'll see our text appear on the page.
### Setting the Application Home Page
Now that we have made the controller and view, we need to tell Rails when we
...
...
@@ -1129,8 +1175,8 @@ you attempt to do just that on the new article form
We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating
articles.
The first step we'll take is adding an
`edit`
action to the
`ArticlesController`
, generally between the
`new`
and
`create`
The first step we'll take is adding an
`edit`
action to the
`ArticlesController`
, generally between the
`new`
and
`create`
actions, as shown:
```
ruby
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录