Tips for sending two objects simultaneously to a template through a Rails Ajax request

Looking to pass two objects to a template that will be rendered after an Ajax call using Rails 3. Take a look at my controller file below.

controller/homes_controller.rb

class HomesController < ApplicationController
    def home
        @sdf=TSdf.new
        respond_to do |format|
          format.html
          format.js
        end
    end

    def scan_report
        @sdf=TSdf.find_by_Receipt_No(params[:sdf][:Receipt_No])
        @hcsy=THcsy.find_by_Sdp_Id(@sdf.Sdp_Id)
    end

In the above controller's scan_report action, I have created two objects to access DB values. Utilizing these objects, I aim to display data in the following template.

view/homes/_hcsy_output_table.html.erb

<div class="block-title">
    <div class="block-options">
        <a href="javascript:void(0)" class="btn btn-option enable-tooltip" data-toggle="block-collapse" title="Toggle block's content"><i class="fa fa-arrow-up"></i></a>
    </div>
    <h4>Harischandra Sahayata Yojana Form</h4>
</div>

<!--Rest of the template code omitted for brevity-->

<p>I am able to retrieve values only through the @sdf object but cannot access values through the @hcsy object, which I require. Please assist me in fetching data by the @hcsy object from the database to this particular template. My other relevant files are provided below.</p>

<blockquote>
  <p>scan_report.js.erb:</p>
</blockquote>

<pre><code>$("#hcsy_block").html("<%= escape_javascript (render 'hcsy_output_table',locals: "{hcsy: @hcsy }" ) %>");
$("#hcsy_block").slideDown(350);

scan_report.html.erb:

<%= render 'hcsy_output_table',locals: "{hcsy: @hcsy }" %>

The _hcsy_output_table.html.erb will render on the home page within div id "hcsy_block". While I can access data through the @sdf object, I also need to fetch data using the @hcsy object. Kindly help me fetch data by the @hcsy object and point out any mistakes made.

Answer №1

The code snippet below is from the home action:

respond_to do |format|
  format.html
  format.js

In the scenario where you have a file called scan_report.js.erb, and in the scan_report action there is no code for the response in js, it raises the question of where the AJAX request originates from and which action it is directed towards. If the request is going to the scan_report action, I recommend adding the following code:

respond_to do |format|
  format.js

If the above solution does not resolve the issue, please provide details on the destination action of the request, as well as the specific code responsible for sending the request.

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

Cross domain request in a simple HTML document

I'm currently working on an app that is strictly in plain HTML files without a server. I'm facing difficulties with cross domain requests from JavaScript. Whenever I try to make a request, the browser displays this error message: XMLHttpRequest ...

Tips for preventing the use of nested functions while working with AJAX?

Consecutively making asynchronous calls can be messy. Is there a cleaner alternative? The issue with the current approach is its lack of clarity: ajaxOne(function() { // do something ajaxTwo(function() { // do something ajaxThree() }); }); ...

Replicating JavaScript functions with the power of Ajax

I'm facing an issue with Bootstrap modal windows on my page. The modals are opening and closing successfully, but the content inside them is fetched through AJAX as HTML. For example, there's a button in the modal: <button id="myBtn"> and ...

Pass user input values to PHP via AJAX and display the outcome on a designated div

I am currently working on a project where I need to send an input value to a PHP script and display the returned value in a div using AJAX. However, I seem to be struggling with getting it to work properly. Any assistance or suggestions would be greatly ap ...

The value of "asp-route-id" is dynamically determined through the use of

Hey there, I’m looking to dynamically pass a value from "codeDrink" to my controller using "asp-route-id" and Ajax in my ASP.NET MVC project. This is how I am handling it in my view: <p> <a id="deleteLink" asp-action="Delete& ...

Displaying success and failure messages on a modal window using Ajax in PHP form

Unable to display error messages or success messages in the same modal window. I have set up Wordpress and using the following code: Form <form id="formcotizador" method="post" action="<?php echo get_template_directory_uri(); ?>/cotizador/procc ...

Learn the process of utilizing AJAX to upload files in CodeIgniter

I have reviewed several solutions How to upload files using ajax in codeigniter File uploads in codeigniter through ajax but none seem to work with my specific code. I am trying to implement a feature where users can upload various types of files (such ...

Discover the power of the "Load More" feature with Ajax Button on

After browsing through the previous questions and experimenting with various techniques, I've come close to a solution but still can't get it to work. The closest example I found is on Stack Overflow: How to implement pagination on a custom WP_Qu ...

Error message: Uncaught TypeError - The function 'send' is not recognized in the Ajax new Request

When I called my ajax function, I encountered the following error: Uncaught TypeError: ajaxGetData.send is not a function Here is my code: <button onclick="myFunc()" class="btn btn-default">Click</button> <div id="getRes"></div> ...

The AJAX process is only retrieving information from the last form within the loop on Django platform

I am facing an issue with a for loop that generates multiple forms on my page. I'm trying to use AJAX to submit these forms without refreshing the page, but no matter which form I submit, it only displays the value from the last form. Here is the HTM ...

What is the best way to iterate through this array in ajax?

Is there a way to properly read this array using ajax? I need some assistance understanding the server response: Array ( [success] => 1 [0] => Array ( [0] => Mr Green [FirstName] => Mr Green ...

Unbelievable - 'Cross-Origin Resource Sharing' headache

When making an ajax request to http://mydomain.com.net/temp/getdata.php?File=something.txt from http://mydomain.com.net/myapp/web/(index.php), I am encountering the following issue: XMLHttpRequest cannot load http://mydomain.com.net/temp/getdata.php?F ...

Encountering an issue with Laravel 5.2: Generating default object from empty value

Currently, I am in the process of developing an application with Laravel 5.2. My objective is to build an Edit modal using jQuery. Unfortunately, each time I attempt to update a record in the database, I encounter a 500 internal server error. Upon investig ...

Using the quote and saying "quotation marks"

Any ideas on how to approach this? This is driving me crazy: $toReturn .= " function addProd(pExists) { document.getElementById('products').innerHTML = \"<tr><td id='prod_n'><input type='text&apos ...

Exploring the differences between detecting the XMLHttpRequest object in JavaScript and using the try

When it comes to determining browser support for AJAX, I typically rely on object detection like this: if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } ...

Angular: When $scope variable is modified, a blank page issue arises

In my Angular application, I have a template called viewAll.html that is displayed after clicking on a link. This template fetches data via AJAX using scope variables. However, I encountered an issue where updating these scope variables through AJAX cause ...

Trouble parsing Ajax response data from web service in JavaScript

When I make an ajax call to a web service, it appears like this - $.ajax({ cache: false, type: 'POST', url: 'callWebService.asmx/getData', dataType: 'json', data: obje ...

The Jquery code encountered an issue where it was unable to access the property 'length' of an undefined

My goal is to submit a form using jQuery and AJAX, which includes file upload functionality. The challenge I'm facing is that the forms are dynamically created, so I need to identify which form was clicked and retrieve its input values. $(document).r ...

Changing a JSON reply to an object using JQuery within Symfony2

I recently delved into the world of AJAX and Json, working on creating a call for a thread of comments to be displayed on my webpage. After the controller finishes loading, it returns an array of objects in Json format: '[{"usr":"gigi","usrpic":"4993 ...

Challenge with neglected open connections from a comet

Utilizing various comet techniques like long polling and forever frame, along with iframes for cross subdomain activities, has presented a challenge during implementation. When a user refreshes the page or navigates to another page, a new request is made w ...