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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:44:50+00:00 2026-06-05T23:44:50+00:00

I am trying to get my posts tagged with a certain tag to render.

  • 0

I am trying to get my posts tagged with a certain tag to render. My code in the view is

Views/posts/sports.html.erb

 <% my_posts = post.find_by_tag("sports") %>
 <%= my_posts.each do |post| %>
    <%= post.title %><br />
    <%= post.body %><br />
 <% end %>

my controller for this looks like

 def sports
     @posts = Post.paginate(:page => params[:page], :per_page => 10)
     @post = Post.find(params[:id])
     @title = "Newest"


     respond_to do |format|
       format.html
       format.json { render :json => @users }
     end
 end

I know I have to define the @post variable but I’m not sure what to define it as.

UPDATE

The problem I’m having is a “Couldn’t find Post without an ID” error

Second UPDATE

   def find_by_tag name 
     Tag.find_by_name(name).posts 
   end
  • 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-06-05T23:44:54+00:00Added an answer on June 5, 2026 at 11:44 pm

    Part of the problem, based on what is shown here, is that you are defining instance variables with Post object(s) in the controller, and then not using them for anything in the view. To retrieve a collection of all the posts tagged “sports,” you’d do the following in the controller:

    @sports_posts = Post.find_by_tag("sports")
    

    and in the view:

    <% @sports_posts.each do |post|
      etc...
    

    To add pagination, you can just chain that method to the original:

    @sports_posts = Post.find_by_tag("sports").paginate(:page => params[:page],
                                                        :per_page => 10)
    

    This is different from your snippet, where you define a @posts variable that returns a collection of 10 Post objects, and @post which simply finds a post object based on the id passed by the submitting form params. My guess is that this controller action is not getting created by a form submission, so no params are passed, and therefore params[:id] is nil, hence the error messages you see. In any event, unless you need either of those items in your view (and there’s nothing here to suggest they’re being used for anything), there’s no reason to create them.

    What you do need is a collection of posts tagged “sports”, which is what the call above accomplishes. It looks like you are trying to do that with post.find_by_tag("sports") in the view. The problem is that you are calling the find_by_tag method on post, which doesn’t exist. Post exists – that’s the Post class, and probably what you mean to be calling. Just changing post to Post would probably get you where you want, but content retrieval and presentation are better separated if you create your objects in the controller and then use the view to simply render their attribute data (per the example above).

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

Sidebar

Related Questions

I am trying to get some form data from POST method. Here's the code
I am trying to only get the posts that are tagged on a tumblr
I'm trying to implement routing such as the following: posts/535434/This-is-a-post-title posts/tagged/tags+here // Matches {controller}/{action}/{id}
I am trying to get post a form to a hidden, dynamically inserted iframe,
I am trying to get a post-commit.bat script running on a Windows Vista Ultimate
I'm trying to get pre/post annotations working with a web application, but for some
I'm stuck trying to get a WinInet HTTP POST via SSL using ONLY C.
I'm trying to get an Android app to post highscores to Facebook, similar to
I am trying to get the following code to work: if (isset($_POST['file'])){ if(file_exists($_POST['file'])){ echo
I'm trying to use the post build command line to get my class library

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.