Ask A Question

Notifications

You’re not receiving notifications from this thread.

Pass variable from Controller to Serializer in Rails

Rony Fhebrian asked in Rails

I have this code in my Controller to get distance in meters from one location to another location:

range = Geocoder::Calculations.distance_between([lat,lng],[Model.location.lat,Model.location.lng])
rangeInMeters = range * 1000            
rangeRounded = rangeInMeters.round
rangeRounded = rangeRounded / 100 * 100

and I have this code in my serializer to give a nicer output for the json response like this:

class ModelSerializer < ActiveModel::Serializer
  attributes :id, :name, :address, :range

  def range
  // something
  end
end

How to get rangeRounded be read by ModelSerializer so in json response it would be like this:

{
        "id": 1,
        "name": "Name A",
        "address": "Address A",
        "range": 500
    },
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.