Thanks for your suggestions Duncan. I would most definitely be happier mapping the Ruby variables to JSON in the controller. But I'm in the middle of a large Rails upgrade and I'm hoping to defer such refactoring improvements, at least until I get this view basically working and (important) get the tests all green. My first step is the conversion to haml.
So... given that it's a suboptimal architecture, how can I take this working erb: <script> var departments = new Object() <% @faculties.each do |fac| %> departments['<%= fac.id -%>']= new Object() <% end %> ... </script> and convert it to haml? On Tuesday, May 21, 2013 9:38:38 AM UTC-7, Duncan Beevers wrote: > > You might be happier using Ruby to map your data to JSON, and then feeding > that to your JavaScript, rather than using Ruby to dynamically generate > JavaScript. > > For example, if you wanted to map the data directly in the view, could do > so like this. > > - mapped_faculties = @faculties.inject({}) { |a, fac| a[fac.id] = {} } > :javascript > var departments = #{mapped_faculties.to_json}; > > In general, I would recommend doing these types of transformations in the > controller rather than in the view itself, but the principle is the same. > > > On Tue, May 21, 2013 at 11:29 AM, Duncan Beevers > <dun...@dweebd.com<javascript:> > > wrote: > >> You're in a javascript filter. The code that you think is iterating >> through @faculties is actually just being inlined as javascript into a >> script tag. >> >> >> On Tue, May 21, 2013 at 11:22 AM, Les Nightingill >> <codeh...@comcast.net<javascript:> >> > wrote: >> >>> I have this in a rails view file: >>> >>> ... >>> :javascript >>> var departments = new Object() >>> - @faculties.each do |fac| >>> departments['#{ fac.id }']= new Object() >>> >>> And the error message I get is: >>> >>> undefined local variable or method `fac' for >>> #<#<Class:0x00000104e1b3d8>:0x000001074bb420> >>> >>> I'm clearly missing something very basic. Can anyone see what the >>> problem is, please. Thanks in advance. >>> >>> Les >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Haml" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to haml+uns...@googlegroups.com <javascript:>. >>> To post to this group, send email to ha...@googlegroups.com<javascript:> >>> . >>> Visit this group at http://groups.google.com/group/haml?hl=en. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> > -- You received this message because you are subscribed to the Google Groups "Haml" group. To unsubscribe from this group and stop receiving emails from it, send an email to haml+unsubscr...@googlegroups.com. To post to this group, send email to haml@googlegroups.com. Visit this group at http://groups.google.com/group/haml?hl=en. For more options, visit https://groups.google.com/groups/opt_out.