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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:42:51+00:00 2026-05-23T15:42:51+00:00

To start out, I know what I am trying to do is not typical

  • 0

To start out, I know what I am trying to do is not typical to Rails. I open to suggestions to better alternatives. I am novice with rails so suggestions are welcome 🙂

Basically, I have a notifications bar that has 5 notifications. After a user clicks and sees the notifications I want to set the a column in the database called seen to true.

Here is my Jquery function:

<script type="text/javascript">
            $(document).ready(function(){
              $("button").click(function(){
                    $("div#count").html("<%= escape_javascript(render('shared/notification_count')) %>");
              });
            });
</script>

And here is the code I am trying to execute only after the click(located in _notification_count.html.erb)

<% notification_ids =   current_user.notifications.map(&:id).join(', ')%>
<% sql = "UPDATE notifications SET SEEN = true where id IN(#{notification_ids})" %>
<% User.connection.select_all(sql) %>

However, it appears that this code is getting executed automatically when the page loads and not after the click. What is my problem?

  • 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-23T15:42:51+00:00Added an answer on May 23, 2026 at 3:42 pm

    Firstly, you cannot simply attach a render to your button to make changes to your database (whether it’s a read or a write). You must make an AJAX call back to your controller using something like

    $.ajax({
        url: "/path/to/your/method",
        type: "GET",
        dataType: "script"
    });
    

    And in your controller you will handle what you need to do, and then render your js file with a

    respond_to do |format|
        format.js {render 'shared/notification_count'}
    end
    

    This is because when a user clicks on your button, the code will be executed on the clients side, but if you never make a request back to the server, then you will not be able to see the updated version of your database. I suggest you reading more on this, that helped me a lot.

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

Sidebar

Related Questions

I'm trying to start out with LinqtoXml. I have added (I think) the right
I'm not trying to start a debate on which is better in general, I'm
Let me start out by saying that I'm not a JavaScript developer so this
When trying to start my JUnit-Test out of Eclipse, I get a ClassNotFoundException. When
Let me start out by saying I have tried this and looked at this
I have been trying to figure out the best practices to write test-friendly code,
I will start with what I am trying to accomplish. I have a ListFragment,
I am trying to use Async controller and am not able to figure out
I'm not trying to start an argument here, but for whatever reason, it's typically
Let me start out by saying that I'm not a C developer and I

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.