Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js.erb not executing javascript but is processed rails

I'm trying to get my pagination working with Ajax, using either will_paginate or kaminari.

When I hit pagination link my log says

Processing by Instructor::FullDashboardController#pupil_leads as JS

and

Rendered instructor/full_dashboard/pupil_leads.js.erb within layouts/main (0.1ms)

But the js has no effect. So to test it I wrote

console.log("Hello");
<%logger.debug "This is the js file"%>
alert('Hello Rails');

In the js.erb file, in my log I see This is the js file as expected, but there is nothing in js console in my browser and the alert doesn't show. So I know the file is being processed, as my logger.debug works but there is no js.

How can I further troubleshoot this?

Rails 4.1

Ruby 2.1

Full Dash Controller

class Instructor::FullDashboardController < ApplicationController
   layout 'main'
...
   def pupil_leads
     @ivar = Model.where("something = ?", some_object.id).order("created_at desc").page(params[:page]).per(10)
     respond_to do |f|
       f.js { render :content_type => 'text/javascript' }
       f.html
     end
  end
like image 775
LpLrich Avatar asked Nov 25 '14 12:11

LpLrich


People also ask

What is JS Erb file in rails?

.js.erb files are for controller actions, such as create, when you want javascript to be executed when the action completes.

Does Ruby on Rails use JavaScript?

Rails uses a technique called "Unobtrusive JavaScript" to handle attaching JavaScript to the DOM. This is generally considered to be a best-practice within the frontend community, but you may occasionally read tutorials that demonstrate other ways.

Where do I put JavaScript ERB files?

Create the js. erb in the view folder itself with the same name as that of the action. In the view where some form is created which will be calling this action, make the request using :remote => true .

What is Rails Ujs?

Rails UJS (Unobtrusive JavaScript) is the JavaScript library that helps Rails do its magic when we use options like remote: true for many of the html helpers. In this article I'll try to explain the main concept of how this works to make it transparent for the user.


1 Answers

Add layout: false option to the render block:

def pupil_leads
   # some code here
  respond_to do |f|
    f.js { render layout: false, content_type: 'text/javascript' }
    f.html
  end
end

For some reason Rails don't recognize request as xhr, I also watched that the views extension (.erb.html or .slim) must be specified in full.

like image 155
Philidor Avatar answered Nov 08 '22 00:11

Philidor