Rails render json add custom attribute

I have a method call:

def group
    render :json => Group.find(params[:id])
end

      

What is he doing:

{
  id: 1,
  name: "Name Here",
  description: "Description Here",
  created_at: "2014-11-24T19:10:53.609Z",
  updated_at: "2014-11-24T19:10:53.609Z"
}

      

I would also like to add a custom attribute that renders the group model. For example, I would like the JSON group to include the attributemessage : "Hello World"

How can i do this?

+3


source to share


1 answer


Adding a method to my model called message

that returns a string and then calls

render :json => Group.find(params[:id]), :methods => :message 

      



did the trick.

+8


source







All Articles