File tree Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -622,9 +622,44 @@ class ArticlesController < ApplicationController
622
622
623
623
` link_to ` 方法是 Rails 内置的视图辅助方法之一,用于创建基于链接文本和地址的超链接。在这里地址指的是文章列表页面的路径。
624
624
625
- 接下来添加指向其他视图的链接。首先在 ` app/views/articles/index.html.erb ` 文件中添加“New Article”链接,把这个链接放在 `
625
+ 接下来添加指向其他视图的链接。首先在 ` app/views/articles/index.html.erb ` 文件中添加“New Article”链接,把这个链接放在 <table > 标签之前:
626
+
627
+ ``` erb
628
+ <%= link_to 'New article', new_article_path %>
626
629
```
627
630
631
+ 点击这个链接会打开用于新建文章的表单。
632
+
633
+ 接下来在 app/views/articles/new.html.erb 文件中添加返回 index 动作的链接,把这个链接放在表单之后:
634
+
635
+ ``` erb
636
+ <%= form_for :article, url: articles_path do |f| %>
637
+ ...
638
+ <% end %>
639
+
640
+ <%= link_to 'Back', articles_path %>
641
+ ```
642
+
643
+ 最后,在 app/views/articles/show.html.erb 模板中添加返回 index 动作的链接,这样用户看完一篇文章后就可以返回文章列表页面了:
644
+
645
+ ``` erb
646
+ <p>
647
+ <strong>Title:</strong>
648
+ <%= @article.title %>
649
+ </p>
650
+
651
+ <p>
652
+ <strong>Text:</strong>
653
+ <%= @article.text %>
654
+ </p>
655
+
656
+ <%= link_to 'Back', articles_path %>
657
+ ```
658
+
659
+ TIP: 链接到当前控制器的动作时不需要指定 : controller 选项,因为 Rails 默认使用当前控制器。
660
+
661
+ TIP 在开发环境中(默认情况下我们是在开发环境中工作),Rails 针对每个浏览器请求都会重新加载应用,因此对应用进行修改之后不需要重启服务器。
662
+
628
663
接着在 ` app/views/articles/show.html.erb ` 模板中添加 ` Edit ` 链接,这样文章页面也有 ` Edit ` 链接了。把这个链接添加到模板底部:
629
664
630
665
``` erb
You can’t perform that action at this time.
0 commit comments