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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:06:43+00:00 2026-05-18T03:06:43+00:00

I am having problems with a remotely executed action and a partial that doesn’t

  • 0

I am having problems with a remotely executed action and a partial that doesn’t update the first time I click the link.

Inside the view (a partial named books) I am creating a link:

link_to "⊗", read_book_path(book), :remote => true

The read_book_path is defined in routes.rb
There is also a conditional that displays a different text when that book is read.

Inside my controller, I have defined a new action:

def read
  @books = Book.all
  @book = Book.find(params[:id])
  @book.read = !@book.read
  @book.save
  respond_to do |format|
    format.html { redirect_to(books_url) }
    format.js {render :layout => false, :locals => { :book => @book } }
  end
end

This means I need a file read.js.erb, this file’s content is:

$("#books").empty().html("<%= escape_javascript( render(:partial => "books") ) %>");

When I click the link, I can see in the terminal window that the database field is updated but the partial is not. Clicking the same link again updates the partial.

Changing the link to :remote => false also works but the page reloads (as expected).

I have tried to debug it with Safari and the Developer tools and I can see the server’s response when clicking the link for the first time.
Something is wrong there, the HTML generated by <%= escape_javascript( render(:partial => "books") ) %> contains the wrong HTML with the old content of the partial. Only the second or third click shows the updated HTML.

I have integrated jquery-ujs – is that the reason the partial doesn’t update the first time or am I missing something else?

This really gave me a headache, can you help me?

Edit:
If that helps: I created a listener in application.js to ajax:before and ajax:complete. The first one shows a little spinner, the second one hides it.
When I click the link, the spinner shows but it doesn’t hide.

  • 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-18T03:06:43+00:00Added an answer on May 18, 2026 at 3:06 am

    It looks like you have an ordering problem that’s causing the trouble. You’re capturing a complete set of books into the @books variable and then modifying a separate copy of a single book. This change will not be propagated back.

    # Load and modify the one book by flipping the flag
    @book = Book.find(params[:id])
    @book.read = !@book.read
    @book.save
    
    # Load all books
    @books = Book.all
    

    As a note this is an extremely inefficient way of doing things, so I hope you’re not working on a large amount of data. You might find it’s easier to do this by simply toggling the one field with a simple UPDATE query:

    Book.update_all({ :read => true }, { :id => params[:id] })
    

    I’m not sure why you’re calling $(...).empty().html(...) instead of simply $(...).html(...) since the html() method should replace the HTML wholesale with no need to clear it in advance.

    One thing that might help is using .js.rjs where the equivalent would be:

    page[:books].replace_html(:partial => 'books')
    

    With simple JavaScript, RJS allows you to eliminate a lot of the drudgery. You can use JS in RJS as well for cases where there is no equivalent:

    page << '$("#books").empty()'
    page[:books].replace_html(:partial => 'books')
    

    To make this more Rails friendly, you could call your partial _book which would make the local variables redundant. Each partial has a default variable with a name matching the template name:

    render(:partial => 'book', :collection => @books)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems using textures that are larger than the OpenGL window or the
I am having problems manually looping through xml data that is received via an
I'm having problems with Iterator.remove() called on a HashSet. I've a Set of time
Im having problems displaying records to my view when passing viewdata to a user
Im having problems building a query with the linq to sql data query expression
I have recently started having problems with TortoiseCVS, or more specifically with plink, the
I'm having problems deciding on what is the best way is to handle and
I am having problems getting text within a table to appear centered in IE.
I am having problems converting a string in the format yyyy-MM-ddZ using VB.net. For
I am still having problems with figuring out how to create winforms in a

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.