Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Problem while using AJAX to render a partial code on the same page

I am new to rails. My project requirement is to load a portion of element on the same page with all elemnts as it is before. For this I have used a new route in routts.rb like

get "/sell_used_car/edit", to:"sell_used_car#edit", as: :sell_used_car_edit

The view of my pain page i.e. of "views/sell_used_car/new.html.erb" is like:

  <%= link_to "Change Email",sell_used_car_edit_path, remote: true %>
  <div id = "content"></div>

In the sell_used_car_controller.rb I have included the codes like:

def edit
    respond_to do |format|
        # format.html{}
        format.js
    end
end

In the above code if I using the line format.html{}, I got error like : "SellUsedCarController#edit is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: []"

If I commented out that line I got the error like : "ActionController::UnknownFormat"

I have placed the file edit.js.erb and _edit.html.erb in right location and that looks like

edit.js.erb

$('#content').html("<%= escape_javascript(render :partial => 'edit')%>");

_edit.html.erb

<%= form_with do |form| %>
<div class="d-flex align-items-center justify-content-center flex-column">
<div class="mb-3">
    <%= form.label :Enter_Your_New_Email%>
    <%= form.text_field :email,  placeholder: "xyz@abc.com", class: "form-control"%>
</div>
</div>
<%end%>

I know if I include the line format.html{}, the error comes because it can't find the .html.erb file. But I have alredy defined in the edit.js.erb that it sholud render to a partail file. But unfortunately it can't. Also I have used only this line in the controller like

def edit
    respond_to do |format|
        # format.html{}
        format.js {render :edit}
    end
end

But the same error comes as "ActionController::UnknownFormat". I am really stuck here.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

With normal link_to you send a html request, probably in console you are getting Processing by SellUsedCarController#edit as HTML.

You need to have rails-ujs and add format: :js in link_to to rails make a ajax request for you installed, for more information: Rails link_to with remote: true processing html instead of js after page refresh.

if you are using more recent version of rails, you can transform you link to into a button make a fetch call via javascript or use turbo to change the content for you.

Example with javascript ajax call:

<button onclick="renderChangeEmail()">
Change Email
</button>

<div id = "content"></div>

<script>
  const renderChangeEmail = () => {
    $.ajax({
      beforeSend: function (xhr) {
        xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
      },
      url: '<%= sell_used_car_edit_path %>',
      remote: true,
      dataType: 'script',
      type: 'get',
    })
  }
</script>
about 4 years ago · Juan Pablo Isaza Report

0

I checked many times to ensure Jquery is properly installed or not. Everytime I found that it is installed properly. As per my last comment, the problem lies in the manifest.json file which I had created manually. So my "public/packs/js" folder is never formed. So I created it manually and put "application-e421b4aa3f716bebdab1.js" and "application-e421b4aa3f716bebdab1.js.map" file in the "public/packs/js folder". And now it is running absolutely fine.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!