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

+0

@Dave私はこのことを自分でコーディングしました(いくつかの本の助けを借りてここに尋ねます)。 – alexchenco

+0

cool!私は投票の例のようなものが必要でした。だから尋ねていた。 – uday

答えて

1

グッドを使用することができます。これは私がやることのいくつかです。私はここにいくつかの例をいくつか挙げました:Partials、Helpers、そしてHTMLをきれいにしてスタイルシートのスタイルをより細かくコントロールすることができました(私は省略しましたが、確かに)。これが私のプロジェクトだったならば、私はさらにすべてを抽出するでしょう(例えば、私はおそらく私が自分の部分やヘルパーに、下の「編集/戻る」リンクと「オーダー」リンクを移動させるでしょう。あなたのコメントがポリモーフィックである(つまり、投稿だけではない)場合は、それを間違いなく独自のビューファイルに移動します。また、私はこれをやや早くやったので、何か誤りがあるかもしれません。

_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

うわー、ありがとう!いいえ、**コメント**は多型ではありません。** vote ** are)。ですから、私は部分的にコメントの部分に投票を含めるべきですか? – alexchenco

+0

はい!アプリケーションの2つの場所に同じコードブロックを置かないでください。書き込み、テスト、および保守は非効率的です。 – bricker

1

あなたはそれをクリーンアップしたいためにあなたにPartials

関連する問題