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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:36:29+00:00 2026-05-11T17:36:29+00:00

In my blog application, some posts appear as excerpts — i.e., the user sees

  • 0

In my blog application, some posts appear as excerpts — i.e., the user sees the first (say) 500 characters, and can click a link to view the entire post. Here is the relevant partial:

<% href = url_for post_path(:id => post) %>

<h1 class="title"><%= post.title %></h1>
<h2 class="published_on"><%= post.author %> wrote this <%= time_ago_in_words(post.published_on)%> ago</h2>
<div class="body">
  <% if defined?(length) %>
    <%= truncate_html(post.body, :length => length, :omission => "&hellip;<h1><a class='more' href=\"#{href}\">Click here for more!</a></h1>") %>
  <% else %>
    <%= post.body %>
  <% end %>
</div>

However, instead of “Click here for more!” taking the user to a separate page, I’d like it to populate the rest of the post inline. Currently, I’ve been implementing this by putting the above snippet in the following div:

<div class="post" id="post_<%= post.id %>">
  <%= render :partial => 'post_content', :locals => { :post => post, :length => 500 } %>
</div>

I then use the id of this div in my application.js to do the AJAX:

$(document).ready(function() {

  $("a.more").click(function() {
    var url = $(this).attr('href');
    var id = url.split("/")[2]
    $.get(url, null, function(data) {
      $("#post_" + id).html(data);     
    });
    return false;
  });

});

This is obviously disgusting — I don’t want my javascript to depend on the location of the post’s id in the link’s href, but I don’t know any other way for the javascript to know which post it is getting and therefore into which div the content should be inserted.

What’s the best way to accomplish this? Should I just go back to using rails’ AJAX helpers?

  • 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-11T17:36:30+00:00Added an answer on May 11, 2026 at 5:36 pm

    Horace, you are correct. You don’t need the Rails helpers at all. To achieve unobtrusive JavaScript Nirvana, you will do well to avoid them (sorry Marc!)

    Unobstrusive JS means avoiding embedded JS where possible. Indeed, try to keep things loosely coupled, but not entirely decoupled. In the example you gave us, it is easy, because we have a URL from the HREF to work with. Your JS does not need to “know” anything about how to request.

    Here is how to send through the link from the HREF’s blindly, and get Rails to respond with an ajax response (i.e. no layout).

    SOLUTION:

    <!------- Your HTML ------>
    <h1 class="title">Schwein Flu Strikes Again</h1>
    <h2 class="published_on">B.Obama wrote this 2 seconds ago</h2>
    <div class="body">
        SUMMARY SUMMARY
        <h1><a class='more' href="/post/1234">Click here for more!</a></h1>
    </div>
    /********* Your JavaScript ***********/
    $(document).ready(function() {

    $("a.more").click(function() {
    $containing_div = $(e).parents('div.body');
    var url = $(this).attr('href');
    $.ajax({
    beforeSend : function(request) { request.setRequestHeader("Accept", "text/javascript"); },
    /* Included so Rails responds via "format.js" */
    success : function(response) { $(containing_div).empty.append(response); },
    type : 'get',
    url : url
    });
    return false;
    });

    });

    ########### Your Controller ###########
    def show
      @article = Post.find(param[:id])
      respond_to do |format|
        format.html { render :action => "show" and return  }
        format.js { render :partial => "post_content", :layout => false and return }
      end
    end

    This also assumes you have RESTful routes or similar to handle /post/:id

    And we’re done! I believe there is something in that for all of us. 😀

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Depending on where exactly you use the environment variable this… May 12, 2026 at 9:25 pm
  • Editorial Team
    Editorial Team added an answer Just put a : before it and a <CR> after.… May 12, 2026 at 9:25 pm
  • Editorial Team
    Editorial Team added an answer This will replace any sequence of carriage-returns (\r) and/or linefeeds… May 12, 2026 at 9:25 pm

Related Questions

In my blog application, some posts appear as excerpts -- i.e., the user sees
I just read a blog post that explains MVC with a banking analogy. I
Some of the answers to a question I had about redirect_to made me think
In my web application, I display the search results using XSLT. There are some
In a fit of unoriginality, I'm writing a blog application using Ruby on Rails.

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.