首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >清除这个show.html.erb视图(Rails)的建议?

清除这个show.html.erb视图(Rails)的建议?
EN

Stack Overflow用户
提问于 2012-02-12 08:20:39
回答 2查看 1.6K关注 0票数 0

以下视图显示了一个帖子及其评论:

views/posts/show.html.erb:

代码语言:javascript
运行
复制
<h2>posts show</h2>

<span>Title: <%= @post.title %></span><br />
<span>Content: <%= @post.content %></span><br />
<span>User: <%= @post.user.username %></span><br />

<div class="post-<%= @post.id %>">
  <h3><span class="vote-count"><%= @post.total_votes %></span> votes</h3><br />

  <div class='voted-user'>
    <% @post.votes.each do |vote| %>
      <%= link_to vote.user.username, vote.user %>
    <% end %>
  </div>

<%= link_to "Vote Up", vote_up_path(@votable, :votable_type => "Post"), :remote => true, :class => "vote-up" %><br />
<%= link_to "Vote Down", vote_down_path(@votable, :votable_type => "Post"), :remote => true, :class => "vote-down" %><br />

<h2>Comments</h2>

<p><%= link_to 'Order by Date', post_path(@post, :order_by => "created_at ASC") %></p>
<p><%= link_to 'Order by Votes', post_path(@post, :order_by => "total_votes DESC") %></p>

<% @comments.map do |comment| %>
  <div class="comment-<%= comment.id %>">
  <p>
    <b>Comment:</b>
    <%= comment.content %>
  </p>
  <p>
    <b>Vote:</b>
    <span class="vote-count"><%= comment.total_votes %></span>

    <div class='voted-user'>
      <% comment.votes.each do |vote| %>
        <%= link_to vote.user.username, vote.user %>
      <% end %>
    </div>
  </p>
  <p>
    <b>Commenter</b>
    <%= link_to comment.user.username, comment.user %>
  </p>
  <p>
    <b>Link</b>
    <%= link_to "Show Post Comment", [@post, comment] %>
  </p>
  <p>
    <b>Vote</b>
    <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %><br />
  </p>
  </div>
<% end %>

<%= will_paginate @comments %>

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :content %><br />
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<% if current_user.id == @post.user_id %>
  <%= link_to 'Edit', edit_post_path(@post) %> |
<% end %>
<%= link_to 'Back', posts_path %>

我只是专注于把事情做好,所以我完全忘了把它弄干净。我是一个Rails初学者,我想要一些建议或建议来清理这个视图(如果您建议将代码移到另一个文件中,请提到文件的名称和目录)。提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-12 09:15:24

很感谢你想把它清理干净。我也会这么做的。我在这里包含了几个例子: Partials、Helpers,还稍微清理了HTML,以允许更多地控制样式表中的样式(我忽略了这一点,但我确信您可以理解这一部分)。如果这是我的项目,我将提取更多的内容(例如,我可能会将entre“编辑/回”链接以及"Order“链接移动到它们自己的部分或助手,因为我可能会在整个应用程序的许多地方使用它们)。如果您的评论是多态的(不只是针对posts),那么一定要将其移到它自己的视图文件中。而且,我做了一个相当快,所以它可能有一些错误,如果你发现任何遗憾。

_post.html.erb

代码语言:javascript
运行
复制
<article class="post-info">
  <span class="title"><b>Title:</b> <%= post.title %></span>
  <p><%= post.content %></p>
  <span class="user"><b>User:</b> <%= post.user.username %></span>
</article>

<div class="post" id="post-<%=post.id%>">
  <div class="vote-count"><%= post.total_votes %></span> votes

  <ul class="voted-user">
    <% post.votes.each do |vote| %>
      <li><%= link_to vote.user.username, vote.user %></li>
    <% end %>
  </ul>

<ul class="voting">
    <li><%= link_to "Vote Up", vote_up_path(post, :votable_type => "Post"), :remote => true, :class => "vote-up" %></li>
    <li><%= link_to "Vote Down", vote_down_path(post, :votable_type => "Post"), :remote => true, :class => "vote-down" %></li>
</ul>

_comment.html.erb

代码语言:javascript
运行
复制
<div class="comment" id="comment-<%=comment.id%>">
    <article class="comment">
    <b>Comment:</b> <%= comment.content %>
    </article>

  <div class="votes-total">
    <b>Votes:</b> <span class="vote-count"><%= comment.total_votes %></span>
  
    <ul class='voted-user'>
      <% comment.votes.each do |vote| %>
        <li><%= link_to vote.user.username, vote.user %></li>
      <% end %>
    </ul>
  </div>

  <div class="commenter">
    <b>Commenter:</b> <%= link_to comment.user.username, comment.user %>
  </div>
  
  <div class="link">
    <b>Link:</b> <%= link_to "Show Post Comment", [comment.post, comment] %>
  </div>
  <div class="vote">
    <b>Vote:</b> <%= link_to "Vote Up", vote_up_path(comment, :votable_type => "Comment"), :remote => true, :class => "vote-up" %>
  </div>
</div> <!-- .comment -->

show.html.erb

代码语言:javascript
运行
复制
<h2>Posts</h2>
<%= render @post %>

<h2>Comments</h2>

<ul class="order">
    <li><%= link_to 'Order by Date', post_path(@post, :order_by => "created_at ASC") %></li>
    <li><%= link_to 'Order by Votes', post_path(@post, :order_by => "total_votes DESC") %></li>
</ul>

<%= render @comments %>
<%= will_paginate @comments %>


<h2>Add a Comment</h2>

<%= form_for([@post, @post.comments.build]) do |f| %>
  <div class="field">
    <%= f.label :content %>
    <%= f.text_area :content %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

<ul class="manage">
    <li><%= edit_link_if_allowed(current_user, @post) %></li>
    <li><%= link_to 'Back', posts_path %></li>
</ul>

员额助理员

代码语言:javascript
运行
复制
def edit_link_if_allowed(current_user, post)
    link_to "Edit", edit_post_path(post) if post.user_id == current_user.id
end
票数 1
EN

Stack Overflow用户

发布于 2012-02-12 08:39:09

你可以用部分数

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9247414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档