I have a simple class that I would like to output as JSON when a user requests a server.
class GeoName
include ActiveModel::Validations
include ActiveModel::Serialization
validates :country_name, :presence => true, :length => {:minimum => 1}
validates :country_code, :presence => true, :length => {:minimum => 1}
validates :province, :presence => true, :length => {:minimum => 1}
validates :district, :presence => true, :length => {:minimum => 1}
validates :zip_code, :presence => true, :length => {:minimum => 1}
validates :city, :presence => true, :length => {:minimum => 1}
validates :street, :presence => true, :length => {:minimum => 1}
attr_accessor :country_name, :country_code, :province, :district, :zip_code, :city, :street
def attributes
@attributes ||= {'country_name' => country_name, 'country_code' => country_code, 'province' => province,'district' => district,'zip_code' => zip_code,'city' => city, 'street' => street}
end
end
In the controller, I do the following:
def index
geoname = GeoName.new
geoname.street = "blablabla"
geoname.city = "blablabla"
render :json => geoname.to_json
end
In routes.rb
controller :testcontroller do
match 'testme', :to => :index
end
When I request the url localhost: 3000 / testme, I get a valid JSON response. However, the console does not work in the console.
rvm/gems/ruby-1.9.3-p125@geo/gems/activesupport-3.1.0/lib/active_support/json/decoding.rb:12:in `decode': [DEPRECATION] MultiJson.decode is deprecated and will be removed in the next major version. Use MultiJson.load instead.
Ruby version 1.9.3-p125 and rails - 3.1.0.