评论效果
多出来两个文本输入框
随便输入点内容,进行提交
代码重构
如果程序中重复代码达到一定量级,会影响可读性和可维护性,这时我们可以将其中重复部分抽出来,单独成块
代码语言:javascript复制[root@h202 blog]# vim app/views/comments/_comment.html.erb
[root@h202 blog]# cat app/views/comments/_comment.html.erb
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
[root@h202 blog]# vim app/views/articles/show.html.erb
[root@h202 blog]# cat app/views/articles/show.html.erb
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<%= link_to 'Back', articles_path %>
| <%= link_to 'Edit', edit_article_path(@article) %>
[root@h202 blog]#