Ruby on Rails - exposing the model properties as json in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails



Exposing the model properties as json

To display address profile fields as markers on a google map, the address field objects need to be passed as json objects to javascript

Regular database attributes

  • When calling to_json on an ApplicationRecord object, the database attributes are automatically exposed.
  • Given a ProfileFields::Address model with label, value, longitude and latitude attributes, address_field.as_json results in a Hash, e.g. representation,
address_field.as_json  # =>
  {label: "Work address", value: "Willy-Brandt-Straße 1\n10557 Berlin",
    longitude: ..., latitude: ...}
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • which is converted to a json string by to_json:
address_field.to_json  # =>
  "{\"label\":\"Work address\",\"value\":\"Willy-Brandt-Straße 1\\n
    10557 Berlin\",\"longitude\":...,\"latitude\":...}"
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • This is useful because it allows to use label and value later in javascript, for example to show tool tips for the map markers.

Other attributes

  • Other virtual attributes can be exposed by overriding the as_json method.
  • For example, to expose a title attribute, include it in the merged as_json hash:
# app/models/profile_fields/address.rb
class ProfileFields::Address < ProfileFields::Base
  # ...

  # For example: "John Doe, Work address"
  def title
    "#{self.parent.name}, #{self.label}"
  end

  def as_json
    super.merge {
      title: self.title
    }
  end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
  • To display address profile fields as markers on a google map, the address field objects need to be passed as json objects to javascript.
  • Regular database attributes
  • When calling to_json on an ApplicationRecord object, the database attributes are automatically exposed.
  • Given a ProfileFields::Address model with label, value, longitude and latitude attributes, address_field.as_json results in a Hash, e.g. representation,
  • address_field.as_json # =>
  • {label: "Work address", value: "Willy-Brandt-Straße 1\n10557 Berlin",
  • longitude: ..., latitude: ...}
  • which is converted to a json string by to_json:
  • address_field.to_json # =>
  • "{\"label\":\"Work address\",\"value\":\"Willy-Brandt-Straße 1\\n
  • 10557 Berlin\",\"longitude\":...,\"latitude\":...}"
  • This is useful because it allows to use label and value later in javascript, for example to show tool tips for the map markers.
  • Other attributes
  • Other virtual attributes can be exposed by overriding the as_json method.
  • For example, to expose a title attribute, include it in the merged as_json hash:
  • # app/models/profile_fields/address.rb
  • class ProfileFields::Address < ProfileFields::Base
  • # ...
  • # For example: "John Doe, Work address"
  • def title
  • "#{self.parent.name}, #{self.label}"
  • end
  • def as_json
  • super.merge {
  • title: self.title
  • }
  • end
  • end
  • The above example uses super to call the original as_json method, which returns the original attribute hash of the object, and merges it with the required position hash.
  • To understand the difference between as_json and to_json, have a look at this blog post by jjulian.

Position

  • To render markers, the google maps api, by default, requires a position hash which has longitude and latitude stored as lng and lat respectively.
  • This position hash can be created in javascript, later, or here when defining the json representation of the address field:
  • To provide this position as json attribute of the address field, just override the as_json method on the model.
# app/models/profile_fields/address.rb
class ProfileFields::Address < ProfileFields::Base
  # ...

  def as_json
    super.merge {
      # ...
      position: {
        lng: self.longitude,
        lat: self.latitude
      }
    }
  end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to exposing the model properties as json in ruby on rails