Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:29:11+00:00 2026-05-29T13:29:11+00:00

The following view shows a single post and its comments: views/posts/show.html.erb: <h2>posts show</h2> <span>Title:

  • 0

The following view shows a single post and its comments:

views/posts/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 %>

I just focused on making things work so I totally forgot to make it clean.
I’m a Rails beginner and I would like some suggestions or advises to clean this view
(if you suggest to move code to another file please mention the name of the file and the directoy). Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:29:12+00:00Added an answer on May 29, 2026 at 1:29 pm

    Good on you for wanting to clean it up. This is some of what I would do. I’ve included some examples of a few things here: Partials, Helpers, and also cleaned up the HTML a little to allow more control over the style in your stylesheets (which I left out, but you can figure out that part I’m sure). If this was my project, I would extract everything even more (for example, I would probably move the entre "edit/back" links at the bottom, as well as the "Order" links, to their own partials or helpers, since I would probably be using them in a lot of places throughout the application.) And if your comments are polymorphic (i.e., not just for posts), then definitely move that into its own view file. Also, I did this a rather quickly, so it might have some errors, sorry if you find any.

    _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>
    

    Posts Helper

    def edit_link_if_allowed(current_user, post)
        link_to "Edit", edit_post_path(post) if post.user_id == current_user.id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following view <asp:Content ID=Content2 ContentPlaceHolderID=MainContent runat=server> <h2>Tables <%=ViewData[RetriverName] %></h2> <%using (Html.BeginForm(ResfreshSelectedTables, Home))
I have the following setup, and the empty view text doesn't show up... protected
I have the following in my view: <%using (Html.BeginForm()) {%> <% foreach (var item
The following view crashes the iPhone If I comment out the using (webView =
I've defined the following view: <CollectionViewSource x:Key=PatientsView Source={Binding Source={x:Static Application.Current}, Path=Patients}/> Where Patient is
Is it ok to do the following: View(new {Object A, Object B}) Or should
I am trying to setup the following view on CouchDB { _id:_design/id, _rev:1-9be2e55e05ac368da3047841f301203d, language:javascript,
In a branch spec, I have the following view: //depot/dev/t/a/g/... //depot/dev/t/r/g/... -//depot/dev/t/a/g/p/o*/... //depot/dev/t/r/g/p/... Perforce
I have the following case where I want to accept the following routs '/type/view/23'
I can set the relationship between View Model and view through following DataContext syntax:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.