Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2

undefined method `map' for nil:NilClass in ruby on rails


when i comment the following code

   <script type="text/javascript">

     // var data1= "<%=[@properties.map {|c| {time:c.time.strftime("%H").to_i , speed:c.speed}}].to_json%>"


    //var data=JSON.parse(data1.replace(/&quot;/g,'"'));

    //alert("dataset-------------------"+data);
</script>

Following error is showing.Why this error occur after i commented the code.While removing comments following error also occurring.

 ActionView::Template::Error (undefined method `map' for nil:NilClass):
        48: <div id="bar-demo" align="center">
        49: <script type="text/javascript">
        50: 
        51:  // var data1= "<%=[@properties.map {|c| {time:c.time.strftime("%H").to_i , speed:c.speed}}].to_json%>"
        52: 
        53: 
        54: //var data=JSON.parse(data1.replace(/&quot;/g,'"'));
      app/views/static_pages/show.html.erb:51:in `_app_views_static_pages_show_html_erb__413296487_31443192

'


Solution

  • The error is occurring when ActionView is trying to evaluate the embedded Ruby. Anything outside of <%= ... %> or <% ... %> has no bearing on any errors that might occur at this stage. In particular the // has nothing to do with anything. Those comments would only have an effect if the embedded Ruby were successfully evaluated by ActionView, the generated HTML document was sent to the client's browser by the controller, and then the HTML gets rendered/JavaScript gets executed by the browser -- only then would the comments have an effect, namely skipping setting the data1 variable.

    Your problem is that at the time the template is being rendered, the instance variable @properties is nil. Do you have something like a StaticPagesController with a show action? In that action do you set @properties anywhere?