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

  • Home
  • SEARCH
  • 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 7072623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:49:52+00:00 2026-05-28T05:49:52+00:00

Ok ill be honest, i haven’t spent much time looking for a solution yet

  • 0

Ok ill be honest, i haven’t spent much time looking for a solution yet seeing as how my son is keeping my attention running around. Either way I would like to ask a question for something that seems pretty simple but has stumped me thus far.

So to keep it simple lets say I have Users(w/model) and Home controllers, Home is the root route.

In the root directory I want to be able to see all posts made by the User using ajax to update a partial on the home page with the list of posts.

In the users controller I have a def called userposts with this in it

def userposts
  @user = User.find_by_id(params[:id])
  @userposts = @user.posts.all(:order => "created_at DESC")
  respond_to do |format|
    format.js { @userposts}
  end    
end

And in my view I have

<p id="aboutuser">
  <% if @user.about? %>
    <%= "   " + @user.id.to_s %>
  <% else %>
    User has not yet filled this out.
  <% end %>
</p> 
<h3 id="authpostlink">
  <%= link_to "List of all posts", user_userposts_path(@user.id), :id => @user.id, :remote => true %>
</h3>

my errors are as follows

Started GET “/users/2/userposts” for 127.0.0.1 at Sun Jan 15 13:36:23
-0600 2012 Processing by UsersController#userposts as JS Parameters: {“user_id”=>”2”} User Load (0.1ms) SELECT “users”.*
FROM “users” WHERE “users”.”id” IS NULL LIMIT 1 Completed 500 Internal
Server Error in 1ms

NoMethodError (undefined method posts' for nil:NilClass):
app/controllers/users_controller.rb:27:in
userposts’

Rendered
/home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(0.8ms) Rendered
/home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(0.8ms) Rendered
/home/n0de/.rvm/gems/ree-1.8.7-2011.03/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (3.2ms)

I do realize i did not post the _show.js.erb file that calls the action to update the div but according to the error messages it doesn’t seem the process has gotten that far.

  • 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-28T05:49:53+00:00Added an answer on May 28, 2026 at 5:49 am

    Assuming you have the following:

    # /app/models/user.rb
    class User < ActiveRecord::Base
      has_many :posts
    end
    
    # /app/models/post.rb
    class Post < ActiveRecord::Base
      belongs_to :user
    end
    

    I would add a nested resource in your routes file:

    #/config/routes.rb
    resources :users do
      resources: posts
    end
    

    You get a bunch of great “_path” methods for free (run $ rake routes from your console to see them all), and it gives you access to URLs such as /users/123/posts. This request will go to the index method of your PostsController and will automatically include :user_id => 123 in the params hash. You can then do the following:

    # In your view:
    <%= link_to "List of all posts", user_posts_path(@user), :remote => true %>
    <div id="posts"></div>
    
    # /app/controllers/posts_controller.rb
    class PostsController < ApplicationController
      respond_to :js # allows for AJAX requests
    
      def index
        if params[:user_id].present? # Do this if using the nested resource
          @user = User.find(params[:user_id])
          @posts = @user.posts.order('posts.created_at DESC')
        else # Otherwise, treat it like a normal request
          @posts = Post.all
        end
        respond_with @posts
      end
    end
    

    Because the your request is sent remotely, you need a corresponding “js” version of your index view (note the file name below and see this Railscast for more explanation):

    # /app/views/posts/index.js.erb
    $('#posts').html("<%= escape_javascript(render(@posts)) %>");
    

    This will render out the posts into that <div id="posts"> tag. (You’ll probably need a “_post.html.erb” partial in /app/views/posts/” as well.)

    However, having said all this, are you sure you need to do this via AJAX? You could simply preload all the posts in the UsersController#show method, initially hide the list using CSS, and then add a jQuery toggle() method on that link. Anyway, hope this makes sense and is helpful.

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

Sidebar

Related Questions

how ill show in the admin section the user picture?, i was looking the
ill have an if statement if (int1 < int2) {} else {} I want
I'm currently developing a TDD idmb html scraper which ill extract certain fields from
Ok title is confusing, Ill tell you my problem first: the polymorphic_url method was
I guess I'm wondering if it is ill-advisable to just set: div { overflow:
This will probably be a bot of a waffly question but ill try my
i would like to know , how ill know which inlines forms was edited?
Probably this isn't the right place... but ill give it a try. I want
I am developing a Reporting Services solution for a DOD website. Frequently I'll have
I am a little out of my depth here, I'll be honest. I am

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.