首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Rails 构建评论功能(6)

Rails 构建评论功能(6)

作者头像
franket
发布2021-10-20 10:06:41
发布2021-10-20 10:06:41
5990
举报
文章被收录于专栏:技术杂记技术杂记

评论效果

多出来两个文本输入框

随便输入点内容,进行提交


代码重构

如果程序中重复代码达到一定量级,会影响可读性和可维护性,这时我们可以将其中重复部分抽出来,单独成块

代码语言: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]# 

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 评论效果
  • 代码重构
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档