Refreshing a div component in Rails by using JavaScript after form submission

In my database, I have a table called "whatsup" and another one called "users". The structure is set up in such a way that each whatsup belongs to a user. Users can create new whatsups using Ajax, as shown below:

$("#chat").append("<%= j render(@whatsup) %>");
$("#new_whatsup")[0].reset();

The newly created whatsups are displayed above the form like this:

<ul id="chat">
  <%= render @whatsups %>
</ul>

<%= form_for Whatsup.new, remote: true do |f| %>
  <%= f.text_field :details%>
  <%= f.submit "Send" %>
<% end %>

In my controller, the declaration for @whatsups looks like this:

@whatsups = current_user.whatsups.all(:limit => 1, :order => 'created_at DESC')

This setup ensures that only one whatsup is displayed at a time.

My goal is to reload the partial @whatsups immediately after the form is submitted, without refreshing the entire page. This way, the newly created whatsups will be automatically shown.

Answer №1

Consider incorporating a JavaScript response into your create action, as shown below:

Inside the WhatsupsController#create method:

respond_to do |format|
    format.js
end

Next, create a new file called create.js.erb in the views/whatsups directory. Here, you can write a piece of JavaScript code that will be executed on the page after the creation process is complete. For example:

$("#chat").append("<%= j render(@whatsup) %>");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Pass on the error to the callback in Node.js

Here is the code snippet in question: User.findById(id, function(err, user) { //blah blah }); The findById method can be found within the User module. Here's a glimpse at its implementation: exports.findById = function(id,callback) { connec ...

Filtering items in a list using Vue JS by different tags

Within my VueJS application, I have a plant array stored in my state. The array structure is as follows: state: { query: '', tag: '', plants: [ { ...

Tips for maintaining an active class on parent nav items in NextJS when navigating dynamic routes

In my nextjs-application, I've implemented a Navbar showcasing several navitems: Navbar.tsx: const Navbar = ({ navitems }) => { return ( <div> {navitems?.map((navitem, idx) => ( <NavItem key={idx} navitem={nav ...

Locate all elements in an array that contain a particular value for a specific key using Javascript

In my JavaScript data, I have two arrays: ARRAY ONE: [ TextRow { v_id: 3000 }, TextRow { v_id: 3001 }, TextRow { v_id: 3002 } ] ARRAY TWO: [ TextRow { s_id: 'S001', v_id: 3000, type: 'control' }, TextRow { ...

When I submit a form with an empty date input field, the Datepicker is sending a 0000-00-00 date

[I am experiencing an issue with my date input field. When I submit without entering any value, the date on the view page shows as 0000-00-00 instead of being empty or blank.] <div class="col-sm-3 col-sm-offset-1"> <div class="input-group"& ...

PostgreSQL dynamic query with Node.js

I am currently developing a note-taking app using REACT. My focus is on tracking only the changes made by the user to the note, rather than the current state of the note. For properties that have not been altered, I intend to send them as an empty string ...

Main navigation category as parent and secondary category as a child menu in WordPress

Hello there, I am looking to create a navigation menu with parent categories displayed horizontally and child categories as corresponding submenus. For example, The parent categories would be Apple, Orange, and Mango Under Apple, we would have apple1, ...

Utilizing Bresenham's line drawing algorithm for showcasing box contours

I am seeking a similar behavior to what is demonstrated on , but with some modifications to the boxes: div { display: inline-block; width: 100px; height: 100px; border: 1px solid black; cursor: pointer; } div.selected, div:hover { backgroun ...

PHP Sessions disappearing during AJAX requests

Currently, I have implemented Facebook login for users on my website. However, I am facing an issue with transferring some session variables to the final page of the application. These sessions are initialized on the Facebook login page. The Facebook Login ...

Dynamic sidebar that adheres to the content and is placed alongside it using JavaScript

I am in need of creating a sticky sidebar that will float beside my content column. The purpose of this sidebar is to contain jump links on the page, similar to what can be seen on this page, but with the navigation buttons positioned directly next to the ...

Iterate through the xml.documentElement in a loop

I want to show 8 men and 2 women in SVG format. However, my current function only displays one man and woman. How can I add a loop to make this work for all individuals? for (i = 0; i < Serie[n].Number; i++) { return xml.documentElement } The data arr ...

Is there a browser-friendly alternative to `fs.readFileSync` function?

When I use fs.readFileSync to read a wasm file in Node, I am able to get the file in the desired format. However, when I attempt the same process in the browser using the FileReader, the format seems to be incorrect. Why does this happen? Is there an equi ...

What causes the appearance of 'GET/ 304 --' in my code? (vue.js, express)

While attempting to fetch data on the client-side using axios in vue.js, I encountered a server-side error with the code 'GET/ 304 --' The reason for this occurrence is unclear to me and I am unsure of how to troubleshoot or resolve it. If I re ...

Jquery ajax success, no data returned despite successful call

I'm in urgent need of assistance. I've encountered an issue with my jQuery code, as I am trying to make an Ajax call to a JavaScript file that contains an array. The JavaScript file is named test-ajax.js var data = ["category", "Alarm"]; r ...

Utilizing PhysiJS for obtaining linear velocity at a specific point on a rotating object

Is there a way to calculate the linear velocity (relative to world space) of a point on an object in PhysiJS, utilizing the object's linear and angular velocity? I considered creating an invisible vertex on the object for this task, but it seems like ...

Despite encountering multiple failures with my Ajax POST request using Express and Plivo, I continue to receive duplicate SMS notifications on my phone

I am currently working with Express, React, Ajax, and Plivo. My issue involves an ajax POST request that I have set up to send data (user phone number and text message) from the client to my express server. While the text message is successfully delivered ...

There was an issue parsing the JSON data due to a syntax error that was encountered

I came across this code snippet: function pushJsonData(productName) { $.ajax({ url: "/knockout/SaveProduct", type: "POST", contentType: "application/json", dataType: "json", ...

Intermittent issues with requesting JSON data

My current project involves building a script that iterates through a list, retrieves product SKUs, and includes them in a JSONP request to retrieve an object. The script seems to be functioning as intended, but there are occasional failures. Here is an e ...

Draggable object animation in VueJS allows for smooth movement of

I'm currently working on developing draggable objects in Vue.js from scratch, but I've encountered two issues. After dragging, the object quickly snaps to the target coordinates without any transition effect. I attempted to eliminate the &apos ...

What is the best way to modify the glyphicon within the anchor tag of my AJAX render property?

Consider the use of Ajax: "target 0" with a render option for an anchor tag. Initially, I am utilizing the class "glyphicon glyphicon-chevron-right". Now, I wish to change it to "glyphicon glyphicon-chevron-down" when clicked. $(document).ready(funct ...