In rails, I am trying to get a URL from a model into my javascript in the view as a string. This is what I do currently.
#in the model
has_attached_file :avatar, styles: { thumb: "60x60>"}, :default_url => "/system/users/avatars/thumb/missing.png"
#using paperclip gem.
....
def someMethod
return [neares.map{|u| u.avatar.url(:thumb)}.compact]
end
Later in the view I do the following:
anArray = <%=aModelInstace.someMethod().to_json%>;
This is what I get in my rendered javascript:
//swap all the $ with &
anArray = [[$quot;/system/users/avatars/thumb/missing.png$quot;],[/* many similar to the previous */]];
This does not compile and causes an error, “unexpected token &”. So how can I get this to return an actual string in javascript, and not just with the wrapped quot signs?
I have tried u.avatar.url(:thumb).to_s and "#{u.avatar.url(:thumb)}". No luck. Also, hours of googling got me nowhere 🙁
As apneadiving and Benjamin Tan said:
Use:
This solved the problem.