69392

how do I access variable attributes using gon (in jQuery & Rails)

Question:

I was recently pointed to gon for passing variables from my controller to javascripts available on a page.

Suppose I pass a variable @note that has attributes location, content, and tags (associated tags). How to I access these attributes in the jQuery file? if I do:

alert(gon.note)

I get an alert [object][Object]

I want to access the content to output a string ie

var string = gon.note.content

gon.note.content doesn't work. Any pointers?

Answer1:

I suppose your @note is an AR instance, then for sure it can't be passed in JS directly, because only simple data types like strings, array, hashes and so on can be exchanged between Ruby and Javascript.

In your case it can be solved quite easily:

# controller gon.note = @note.attributes # returns a hash # JS var content = gon.note.content;

Recommend

  • MySQL multiple IN conditions to subquery with same table
  • Is it possible to get the word under the mouse cursor in a ``?
  • BeautifulSoup difference between findAll and findChildren
  • NHibernate manually control fetching
  • Zoom in and out of jPanel
  • Firefox Extension - Monitor refresh and change of tab
  • How to use JavaScript to determine whether a file exists in a directory?
  • Saving Changes After In-App Purchase Has Been Purchased
  • Functions in global context
  • FileReader+canvas image loading problem
  • Align navbar back button on right side
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Deserializing XML into class C#
  • How to set my toolbar fixed while scrolling android
  • To display the title for the current loaction in map in iphone
  • jQuery tmpl and DataLink beta
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Compare two NSDates in iPhone
  • How to disable jQuery.jplayer autoplay?
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • using HTMLImports.whenReady not working in chrome
  • coudnt use logback because of log4j
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Authorize attributes not working in MVC 4
  • apache spark aggregate function using min value
  • python draw pie shapes with colour filled
  • EntityFramework adding new object to nested object collection
  • Sorting a 2D array using the second column C++
  • How to Embed XSL into XML