2012-02-12 39 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