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 1059021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:06:39+00:00 2026-05-16T18:06:39+00:00

How would you refactor this logic filled partial? <%- for post in @posts -%>

  • 0

How would you refactor this logic filled partial?

<%- for post in @posts -%>
  <div class="post">
    <%= link_to post.title, post %>

    <%- if post.name.empty? -%>

    <%- else -%>   
      <span class="name">
        by 
        <%- if post.email.blank? -%>
          <%= post.name %>
        <%- else -%>
          <a href="mailto:<%= post.email %>"><%= post.name %></a>
        <%- end -%>
      </span>
    <%- end -%>

    <span class="time">
      active &#32; <%= timeago(post.updated_at) %>
    </span>

    <%- if post.comments.empty? -%>
      <span class="reply">
        <%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
      </span>
    <% else %>
      <span class="reply">
        <%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>
      </span>
    <%- end -%>

    <p><%= sanitize post.content,
 :tags => %w(a embed img object p param),
 :attributes => %w(allowfullscreen allowscriptaccess href name src type value) %></p>

    <%- unless controller.controller_name == "tags" -%>
      <%- unless post.tag_list.nil? || post.tag_list.empty? -%>
        <%- post.tags.each do |t| -%>
          <div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
        <%- end -%>
      <%- end -%>
    <%- end -%>

    <%- unless post.comments.empty? -%>
      <div class="comments">
        <%= render :partial => post.firstcomments %>
        <%- if post.comments.count >= 4 -%>
          <%= link_to 'more...', :action => 'show', :id => message %>
        <%- end -%>
      </div>
    <%- end -%>
  </div>
<%- end -%>

Notes:
post.firstcomments just returns the 3 latest posts.
Using Rails 3 and Ruby 1.9.2.
I haven’t looked into the sanitize portion of the code, and I realize Rails 3 escapes everything by default, so it can safely be ignored for now unless someone knows how to convert it.

My models are clean and my controllers are decent but this partial is awful. It does what it needs to do but it takes up most of the rendering time when refreshing the page. Comments, suggestions and code are much appreciated. Thank you for reading my question.

  • 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-16T18:06:40+00:00Added an answer on May 16, 2026 at 6:06 pm

    I don’t know if I want to wade my way through that but I’ll get you started.

    <%- if post.name.empty? -%>
    <%- else -%>   
      <span class="name">
        by 
        <%- if post.email.blank? -%>
          <%= post.name %>
        <%- else -%>
          <a href="mailto:<%= post.email %>"><%= post.name %></a>
        <%- end -%>
      </span>
    <%- end -%>
    

    could be refactored to something like this:

    <%= by_post_name post %>
    
    #post_helper.rb
    def by_post_name post
      post.name.empty? ? "" : "<span>#{post.email.blank? ? post.name : mail_to post.email, post.name}</span>"
    end
    

    Simple things like this:

    <%- if post.comments.empty? -%>
      <span class="reply">
        <%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
      </span>
    <% else %>
      <span class="reply">
        <%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>
      </span>
    <%- end -%>
    

    is the same as:

    <span class="reply">
      <%- if post.comments.empty? -%>
        <%= link_to 'reply', :controller => 'posts', :action => 'show', :id => post %>
      <% else %>
        <%= link_to pluralize(post.comments.count, 'reply'), :controller => 'posts', :action => 'show', :id => post %>   
      <%- end -%>
    </span>
    

    also help. Stick your empty comment logic in a helper method as I did in my first example.

    Really none of this is going to help with the rendering time. Refactoring view logic is primarily for the benefit of yourself and anyone who has to read your code and is unlikely to make any difference in speed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how would I refactor this code for ARC: - (UIGestureRecognizer *)createTapRecognizerWithSelector:(SEL)selector { return [[[UITapGestureRecognizer
I have trouble finding way to correctly refactor this code so that there would
Quick question. How would you refactor this Asp.net MVC 2 HtmlHelper? Specifically would it
Given the following code, How would you refactor this so that the method search_word
Having 2 different interfaces is a must. How would you refactor this? Should I
How would you refactor this code, with respect to the LINQ? I'm new to
How would you refactor this, keeping in mind that you have dozens more of
How would I refactor this to get it to return a string not a
How would you refactor something like this: $start = $_GET['s']; if (!is_int($start) || $start
I'm trying to refactor this algorithm to make it faster. What would be the

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.