0

I like to add a different way json data is rendered in Rails.

The problem is is that i don't want all the field names for each row, just a array with data for each column, moreover it would be nice to setup some type of filter so you can select which columns you want in the JSON output

At the moment i have in a controller:

respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @person }
end

And i would like to do something like:

format.json { render :json2 => @person }

So that in this specific action of a controller, the JSON data is formatted differently (:json2). I was looking at:

ActionController.add_renderer :json2 do |json, options|

But i can't wrap my head around this. Were do i start? I do want it to run it in the same way as the 'default' json renderer. So no separate index.json2.erb files in each view. Even better if i can add some extra fields to the array like:

format.json { render :json2 => @person, :table = 'xyz', :columns = ['a', 'b'] }

I did read Yehuda Katz but i still can't figure it out.

Roger
  • 7,535
  • 5
  • 41
  • 63
  • downvote? for what? i did read the other articles but that was for Rails 2. I just started with Rails 3, and since the differences... – Roger Mar 06 '12 at 22:06

1 Answers1

1

I think it would be great for you to override the to_json method. Here a similar question.

How to override to_json in Rails?

Community
  • 1
  • 1
damienbrz
  • 1,806
  • 2
  • 11
  • 15