高级Web应用开发实战:控制器、模板与授权优化
1. 优化文章索引页面
当前文章索引页面使用表格标记来列出文章,这种方式不够友好,我们可以使用 app/views/articles/_article.html.erb 中的文章部分来更新标记。以下是更新后的文章索引代码:
<h1>Listing articles</h1>
<div id="articles">
<%= render @articles %>
</div>
<br />
<%= link_to 'New article', new_article_path %>
注意,在重用部分时要小心,有时可能更适合保留单独的文件。访问 http://localhost:3000 ,如果一切正常,你会看到一个类似真实博客的页面。
2. 为文章表单添加分类
之前已经为文章模型添加了分类,但控制器和模板还不知道。现在从文章表单开始解决这个问题,将以下代码添加到 app/views/articles/_form.html.erb 中:
<%= form_for(@article) do |f| %>
<% if @article.errors.any? %>
<div id="error_explanation">
超级会员免费看
订阅专栏 解锁全文
5万+

被折叠的 条评论
为什么被折叠?



