Submit form data via ajax to update category object

I have been working on creating an index page to display all categories with their respective names and buttons for editing and deleting each category. My goal is that when I click on the edit button, the row should transform into a form with a text field pre-filled with the category name, along with a save button. So far, I have managed to implement the functionality to display the edit form within the row upon clicking the edit button. However, when I click the save button, nothing happens - absolutely nothing at all. There are no errors in the console, no network calls being made, and no activity shown in the server log. I believed that I had included all the necessary code to handle the save action, but now I am unsure where to troubleshoot. Could someone please offer some guidance? My application uses Rails version 4.2.5.

Application.js

// Functionality for displaying edit form inside the row
document.addEventListener("turbolinks:load", function() {
      $("[name='edits']").on('ajax:complete', function(event, data, status) {
        $(this).parent().parent().html(data.responseText)
      })
    })

    // Handling click on save button
    document.addEventListener("turbolinks:load", function() {
      $('#edit-form').on('ajax:complete', function(event, data, status){
        $('#categories').html(data.responseText);
      })
    })

Category controller:

def edit
    render partial: 'categories/edits'
  end

  def update
    if @category.update(category_params)
      flash[:success] = "Category #{@category.name} was saved successfully"
    else
      flash[:danger] = "There was an error, please try again!"
    end
    render partial: 'categories/list'
  end

Edit form partial:

<div>
  <%= form_tag category_path, remote: true, method: :put, name: "edit-form" do %>
    <div class="form-group row text-center col-md-12">
      <div class="col-md-10">
        <%= text_field_tag :name, @category.name, 
                                  placeholder: "Category Name", autofocus: true,
                                  class: "form-control" %>
      </div>
      <div class="col-md-2">
        <%= button_tag(type: :submit, class: "btn btn-primary") do %>
          Save
        <% end %>
      </div>
    </div>
  <% end %>
</div>

In reviewing the HTML, I noticed that the input field and button are placed outside the form tags. Could this factor be contributing to the issue? It currently looks like this:

<form></form>
<input>
<button></button>

Answer №1

It seems like the issue was caused by placing the form inside the tr tag. Moving it to the td element resolved the problem.

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

Is there a way to display a 404 error in Vue without altering the URL?

Query I'm trying to figure out how to display a 404 error in a dynamic route using Vue.js without changing the URL. Can anyone help? This is part of my route.js file (everything seems fine) { path: '/work/:id', name: 'work& ...

Automate page refresh using Selenium in Java until target element becomes visible

Is there a way to have selenium wait for 3 minutes before refreshing the page until a specific element (in this case, the Download button) is found? I've attempted the code below, but it doesn't seem to be effective. Note: I am trying to upload ...

JavaScript allows you to export a variable from a function

I simply want to access the value of the userId variable for any use. async function getMyData(){ const token = ''; spotifyApi.setAccessToken(token); const data = await spotifyApi.getMe(); let userId = data.body.id; retu ...

Tips for eliminating unwanted white space underneath your webpage while zooming on a mobile device

Currently developing a responsive website, everything is running smoothly except for one issue. When I pinch zoom in on mobile and scroll down, a large white bar appears below the entire page, stretching across the screen width. How can this be fixed? Belo ...

The issue with Firefox's DOMContentLoaded event

After creating a script that interacts with the Dom, I noticed that it needs to wait until the Dom is ready before executing each operation. My intention is to make this script usable in two ways: Include it in the head tag, ensuring it loads before the ...

storage location for data in JSON format

When using content type : "application/x-www-form-urlencoded", the result is stored in Request.Form in Asp MVC. However, for "application/json", the storage location cannot be found. Below is the code that is being used: AJAX part // reading form da ...

Saving a single ID at a time in a UseState array by clicking in ReactJS

When clicking on the "click here" button, I am trying to save favorite post IDs in an array. However, the issue is that it is currently only saving one ID at a time in the array. Every time you click on another "click here" button, it removes the previous ...

Guide to selecting text within an unordered list on an HTML webpage without a distinctive identifier with Python Selenium

I am looking to automate the selection and clicking of the text 'Click Me Please' within a hyperlink using Python Selenium. The main challenge is that there is no distinct identifier for this item as it is nested within a list. The specific HTM ...

How does the 'v-show' command cause unexpected changes to the normal layout of HTML elements (such as the position of divs)?

I'm looking to create unique custom components, similar to the one shown in this image: https://i.sstatic.net/MLj1a.png Just to clarify, this is a component from an element(). When I hover over the picture, it displays a translucent background. So ...

Display information from a mysql database table within a selection menu

Currently, I am working on a dropdown menu that should display data from a MySQL table. However, I am facing an issue which is outlined below: In my PHP script, I have approached it in the following manner: <label class="col-form-label" for="formGrou ...

Adjust the width of the TinyMCE Editor to automatically resize based on the content being

Is it possible for TinyMCE to adjust the content within an absolutely positioned container and update the width while editing? <div class="container"> <textarea>This is my very long text that should not break. This is my very long text tha ...

Struggling to get JQuery timing events to function properly?

Encountering timing issues with the events in my self-made simon memory game. Experimented with setTimeout but struggling to figure out which events to time and the appropriate duration for them to be distinguishable. $('#play').click(function( ...

Execute JavaScript code and showcase the result in the frame on the right side

On the left side of a frame, I have a menu. My goal is to invoke a javascript function that will load a new HTML page on the right side of the frame using XML data and an XSL template. When trying to achieve this with fixed parameters, the code works perf ...

Establish a Connection Between Local Mongo Database and Your Application

I have successfully set up a local MongoDB connection with a React, GraphQL application. All configurations are in place and functioning properly as far as I can tell. To visually view my MongoDB databases, I have installed Compass. The content of the Ser ...

Issue: The system is unable to locate the module titled '@kyleshockey/object-assign-deep'

Recently, I installed the accept-language npm module. When attempting to start my project afterwards, an error message appeared: Error: Cannot find module '@kyleshockey/object-assign-deep' What steps can I take to fix this problem? ...

Ensuring that non-English characters are accepted in jQuery validation using regular expressions

I have a validator for checking website URLs on my site, and it works perfectly fine. However, I now need to enable the input of non-English characters like Arabic. I have some knowledge about regular expressions, but I am unsure about how to allow Arabic ...

Refreshing javascript in order to crop images

I'm currently working on developing a function that allows users to select an image for their avatar, preview it on the page using JavaScript, crop it using the jQuery Guillotine plugin, and then upload it to the server with the necessary coordinates ...

jQuery - class remains unchanged on second click event

Operations: Upon clicking on an element with the class thumb_like or thumb_unlike, a like will be added or removed for the image using a POST request. The element with the class thumb_count will increase or decrease based on user actions. For example, lik ...

Ways to transfer data between components in Angular 2: Passing variables between children

Within my application, I have a parent component (P) and two children components (C1) and (C2). Both C1 and C2 are children of the parent component P, but they are not nested. In the parent template, it is structured as follows: <c1-component></c ...

Removing children from collection using JQuery selector based on class attribute

I am facing a bit of a challenge with a JavaScript plugin that exports HTML tables into Excel. I am not an expert in JavaScript, so I hope someone can help me out. The issue lies in the fact that the plugin excludes table rows based on a certain class, but ...