English 中文(简体)
.js.erb 中的无方法错误
原标题:NoMethodError in .js.erb

我正试图写出不受侵扰的标语 我的看法是:

<%= javascript_include_tag "trend" %>

在我的潮流中,Js.erb有:

$(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"

感谢您,感谢您

编辑 :

这里为实际错误 :

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">

我有控制器 统计与行动趋势

最佳回答

我猜是 @ testss 在一个控制器中定义 。

资产管道没有被动作控制器编译, 也没有被操作控制器触动过, 所以... 您不能在管道中您指定的控制器中设置实例变量 。

周围的工作包括 @testsss.to_json, 在您看来,

类似的东西

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

then in your javascript do 类似的东西 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)} });
     }); 
  });

或者您可以在视图中而不是资产管道中 使js在视图中成为js

#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>

这样做的好处在于您在 js 中的动作控制器中指定了实例变量,但您主页加载时间较长的缺点。

我挑了第一个办法,但由你决定吗?

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签