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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:02:57+00:00 2026-05-14T09:02:57+00:00

I have built a blog application using Ruby on Rails. In the application I

  • 0

I have built a blog application using Ruby on Rails. In the application I have posts and tags. Post has_many :tags and Tag belongs_to :post.

In the /views/posts/index.html view I want to display two things. First is a listing of all posts displayed ‘created_at DESC’ and then in the side bar I am wanting to reference my Tags table, group records, and display as a link that allows for viewing all posts with that tag.

UPDATE:
The issue of duplicate posts being displayed and posts w/o tags not displaying have been fixed. Just trying to figure out now, how to handle the /posts?tag_name=foobar request such that only the posts with that tag are displayed.

UPDATED CODE:
The posts are displaying properly, no duplication. The tag count is working correctly, and the tag groups are displaying as links and passing the tag_name into /posts?tag_name=new. I just can’t get the link to trigger the display of only the posts that have those tags. FYI, the posts are identified in the tag table by post_id.

PostsController

 def index
    @tag_counts = Tag.count(:group => :tag_name, :order => 'updated_at DESC', :limit => 10)
    @posts = Post.all( :order => 'created_at DESC' ).paginate :page => params[:page], :per_page => 4,
              :conditions => (params[:tag_name] ?
                { :tags => {:tag_name => params[:tag_name]} } : {}
                  )

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
      format.json { render :json => @posts }
      format.atom
    end
  end

View

Recently Used Tags
        <table>
            <% @tag_counts.each do |tag_name, tag_count| %> 
                <tr>
                    <td><%= link_to(tag_name, posts_path(:tag_name => tag_name)) %></td>
                    <td>(<%=tag_count%>)</td>
                </tr>
            <% end %>
            </table>
            <br>
            <a href="/tags">click to view all tags >></a>
            </div>

This screen shot might help (please note it is really ugly as just working on function) as it shows that there is only 1 tag in new but when the URL is hit, still displaying all posts.

alt text
(source: squarespace.com)

  • 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-14T09:02:57+00:00Added an answer on May 14, 2026 at 9:02 am

    To get all posts:

    @posts = Post.all.paginate :page => params[:page], :per_page => 5
    

    To show the post’s tags:

    <% @posts.each do |post| %>
       <% post.tags.each do |tag| %>
          <%= tag.tag_name %>
       <% end %> 
    <% end %>
    

    I believe this is what you want.

    Edit #1

    To show the tags in the side bar:

    <% Tags.count(:group=>"tag_name").each_pair do |key, value| %>
        <% link_name = Tags.find( key ).tag_name %>(<%= value %>)
        <%= your_link %>
    <% end %>
    

    This will show all the tags with the number of posts between ( ). But I don’t know how to build your link… Maybe if you give some more info, i don’t know… Do you have a search method?

    Anyway… hope it helps you :]

    Edit #2

    Looking another question you’ve made and seeing some code, I believe this will solve your problem:

    #view
    <% Tags.count(:group=>"tag_name").each_pair do |key, value| %>
       <% tag_name = Tags.find( key ).tag_name %>
       <%= link_to(tag_name+"(#{value})", posts_path(:tag_name => tag_name)) %>
    <% end %>
    
    #controller
    @posts = Post.all.paginate :page => params[:page], :per_page => 5,
       :conditions => (params[:tag_name] ?
          { :tags => {:tag_name => params[:tag_name]} } : {}
       )
    

    Hope it works now.

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

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are a few ways you could determine whether your… May 14, 2026 at 8:11 pm
  • Editorial Team
    Editorial Team added an answer Check isUserInteractionEnabled on the view. Some views, like UIImageView, disable… May 14, 2026 at 8:11 pm
  • Editorial Team
    Editorial Team added an answer Set the same image for UIControlStateDisabled and UIControlStateNormal so the… May 14, 2026 at 8:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.