On Jul 23, 9:52 am, Max Williams <[email protected]>
wrote:
> vlain wrote:
> > Wrap variables around <%= %> inside double quote
> > e.g.
> > for(var j=0; j < "<%=locations.length%>"; j++)
>
> > On Jul 22, 4:17 pm, Nilesh Kulkarni <[email protected]>
>
> As a slightly more general solution, rather than wrapping the "" around
> the erb block, if you call inspect on the object inside then it will
> have quotes added if it's a string but not if it's a number, and if it's
> an array it will be shown in the right format, etc. So, .inspect makes
> things print out in such a way that they 'look right' and can be used as
> is.
>
I would use to_json since that is explicitly making things that
javascript understands - it's mostly a coincidenance that inspect
coincides with this for certain types objects (for example with a hash
you need to_json)
Fred
> (In the example above it looks like you need a number rather than a
> string. )
>
> #in rails controller
> @cars = Car.find(:all)
>
> #in your js section in a view page
> var carNames = <%= @cars.collect(&:name).inspect %>;
> => var carNames = ["Micra", "Primera", "Almeira"];
>
> var firstCarModel = <%= @cars.first.model.inspect %>;
> => var firstCarModel = "Nissan";
>
> for(var j=0; j < <%= @cars.length.inspect %>; j++)
> => for(var j=0; j < 3; j++)
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---