Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7992235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:30:15+00:00 2026-06-04T13:30:15+00:00

I am trying to write unobtrusive javascript, and I have in my view: <%=

  • 0

I am trying to write unobtrusive javascript, and I have in my view:

<%= javascript_include_tag "trend" %>

and in my trend.js.erb there is:

$(document).ready(function() {

    <% @testsss.each do |t|%>

        $('#<%= "t-"+t.id.to_s %>').click(function(event){
            //alert("sometext");
            $.ajax({type: "GET", url: "/returngraph?test=<%= t.id.to_s %>", dataType: "html",success: 
            function(data){ $("#chart").html(data)} });

        }); 

    <% end %>


});

But I got NoMethodError…
what I am doing wrong? @testsss is set of objects and same @testsss.each works in trend.html.erb from where I call javascript_include_tag “trend”

thank you

edit:

here is actual error:

NoMethodError in Statisticall#trend

Showing /xxxx/app/views/statisticall/trend.html.erb where line #1 raised:

undefined method `each' for nil:NilClass
  (in /xxxx/app/assets/javascripts/trend.js.erb)
Extracted source (around line #1):

1: <%= javascript_include_tag "trend" %>
2: <div id="hhead">

I have controller Statisticall and action trend

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:30:17+00:00Added an answer on June 4, 2026 at 1:30 pm

    I am guessing @testsss is defined in a controller.

    The asset pipeline is not compiled or ever touched by action controller so… you cannot have instance variables you assign in your controllers in the pipeline.

    Work arounds include @testsss.to_json and including that as a JS variable in your view

    something like

    #app/views/some/view.html.erb
    <script type='text/javascript'>
     window.testsss = <%= @testsss.tojson %>
    </script>
    

    then in your javascript do something like this using underscore.js

      _(testsss).each(function(t){
        $('#t-'+ t.id).click(function(event){
             //alert("sometext");
             $.ajax({type: "GET", url: "/returngraph?test=" + t.id, dataType: "html",success: 
             function(data){ $("#chart").html(data)} });
         }); 
      });
    

    or you can render that js inside a view rather than the asset pipeline

    #app/views/some/view.html.erb
    <script type='text/javascript'>
      $(document).ready(function() {
        <% @testsss.each do |t|%>
          $('#<%= "t-"+t.id.to_s %>').click(function(event){
            $.ajax({
              type: "GET",
              url: "/returngraph?test=<%= t.id.to_s %>",
              dataType: "html",
              success: function(data){ $("#chart").html(data)}
            });
          }); 
        <% end %>
      });
    </script>
    

    Doing it this way gives you the advantage of having instance variables assigned in action controller in your js but the disadvantage of longer loading of your main page.

    I perfer the first approach but that’s up to you?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In trying to write more testable Java code, I have been using the Model-View-Presenter
I am trying write a function that generates simulated data but if the simulated
Trying to write a function to see how often an object exists and give
Trying to write a javascript bookmarklet to jump from a front end link into
Im trying to write something to get my images to show correctly. I have
i'm trying to write unobtrusive default/placeholder text in input (actually, relatively placed label over
Im trying to write a recursive function which returns the factorial of a number.
I am trying to write some javascript functions for a web application. Following the
Trying to write a matrix-multiplying function for arbitrary-sized matrices in C. I'm trying the
Trying to write function that numerize items of list and returns list of tuples

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.