2012-02-12 41 views
0

以下视图显示了单后,其意见:建议清洁这个show.html.erb视图(Rails)?

的意见/职位/ show.html.erb:

<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初学者,我想要一些建议或建议来清除此视图 (如果您建议将代码移动到另一个文件,请提及文件名称和directoy)。提前致谢。

+0

你在哪里找到这个例子?(我的意思是在哪本书中)? – uday 2012-02-12 08:28:11

+0

@Dave我自己编写了这个东西(由一些书帮助并在这里问)。 – alexchenco 2012-02-12 08:38:31

+0

酷!我需要类似投票的例子。所以问了。 – uday 2012-02-12 08:41:27

回答

1

好于你想要把它清理干净。这是我会做的一些事情。我在这里包含了一些例子:Partial,Helpers,并且还清理了一点HTML以允许更多地控制样式表中的样式(我忽略了这一点,但是您可以弄清楚我的那一部分当然)。如果这是我的项目,我会提取一切甚至更多(例如,我可能会将底部的entre“edit/back”链接以及“Order”链接移动到他们自己的部分或帮助程序中,因为我可能会在整个应用程序中的很多地方使用它们。)如果您的评论是多态的(即,不只是针对帖子),那么一定要将其移到它自己的视图文件中。另外,我做了这个很快,所以它可能会有一些错误,如果你发现任何错误,很抱歉。

_post.html.erb
<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
<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
<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> 

帖子助手

def edit_link_if_allowed(current_user, post) 
    link_to "Edit", edit_post_path(post) if post.user_id == current_user.id 
end 
+0

谢谢你!不,**评论**不是多态的,**票**都是)。那么,我是否应该在评论部分中包含部分投票? – alexchenco 2012-02-12 09:33:54

+0

是的!永远不要在你的应用程序的两个地方有相同的代码块。编写,测试和维护效率不高。 – bricker 2012-02-12 12:08:29