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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:00:17+00:00 2026-05-27T00:00:17+00:00

I have a story model that has many posts. I list the stories in

  • 0

I have a story model that has many posts. I list the stories in an index view without each story’s posts. When a user clicks on one of the stories in the index view I want to load that story’s posts via calling stories/(story_id)/posts using the ajax .load function, and make sure to put the posts into that story’s correct div (created by “<%= div_for(story, “posts_bucket_for”) do %>”)

The problem is, I don’t know how to pass the correct story_id to the .load function or how to make sure that the resulting posts are loaded into the correct posts_bucket_for.

Here’s my _story.html.erb file:

<%= div_for(story) do %>
   <%= div_for(story, "heading_for") do %>
        <span class=story_title>
            <%= story.title.titleize %>
        </span>
        <span class=story_post_amount>
            # of posts: <%= story.posts.length %>
        </span>
   <% end %>
<%= div_for(story, "posts_bucket_for") do %>
<% end %>

Here’s my PostController#index action

  def index
    @story = Story.find(params[:story_id])
    @posts = @story.posts
    respond_with(@posts, :api_template => :public, :root => :posts)
  end

Any help would be greatly appreciated!!!

  • 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-27T00:00:18+00:00Added an answer on May 27, 2026 at 12:00 am

    Here is one way to implement a solution (this is for Rails 3).

    According to the Rails documentation for div_for, div_for(story, :class => "story") will produce HTML that looks like:

    <div id="story_123" class="story"> Your content </div>  
    

    So, with jQuery, you could run something like:

    // Attach click event handlers to your story divs
    $(function() {
      $("div.story").click(function() {
        // Get the HTML ID of the story div
        html_id = $(this).attr('id');
        // Extract the database id
        story_id = html_id.split("_")[1];
        // Call your handy function to load story posts
        loadStoryPosts(story_id);
      });
    });
    

    For your buckets, I would not use another div_for(story). This would result in you having two divs on the page with the same ID. Instead, I would do something like this:

    <%= div_for(story, :class => "story") do %>
        <p>The story</p>
        <div class="posts" />
    <% end %>
    

    And then, in jQuery

    // Make an AJAX request to your Rails server
    function loadStoryPosts(story_id) {
      // This is a jQuery AJAX GET request to your stories/:id/posts route
      $.get("stories/" + story_id + "/posts", function(data) {
        html_id = "story_" + story_id;        
        // Insert the result of the request into the appropriate posts bucket
        $("#"+html_id+" .posts").html(data);
      });
    }
    

    The remaining task is to have a controller action handle the AJAX request and return the posts to be inserted onto the page.

    class StoriesController < ApplicationController
      def posts
        story = Story.find params[:id]
        # Render the story's posts inside a partial (without any layout)
        render :partial => "stories/posts", :locals => { :posts => story.posts }, :layout => false
      end
    end
    

    You’ll also want to create a partial to render the posts. In my example, this would be views/stories/_posts.html.erb.

    This is a lot of new learning material if you haven’t dealt with AJAX+Rails before, so here are a couple useful tutorials: here and here.

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

Sidebar

Related Questions

I have a store model that has many products with a has_many :through relationship.
I have a event model that has many invitations. Invitations are setup through checkboxes
I have a model- Link - that has many hits. What I'm trying to
I'm building an application that has a Keynote model and a Story model (as
I'm building an application that has a Keynote model and a Story model (as
I have created a core data model that has two entities which have a
I currently have an activity model that handles a user activity notification system. An
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have a collection of stories that user likes, i want to paginate it.
I have a number of models that store words as a string with each

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.