Daniel
Daniel

Reputation: 4314

Ruby on Rails: render action return value when format is json

I've got 31 method ending with render :json => blah_blah_blah, thats annoying.

My idea is to set default response type and render actions return value. First part is done:


before_filter do
  request.format = :json unless params[:format]
end

but I can't make up anything with second part of this idea. I've tried around_filter, but it doesn't get return value, tried to play with default_render, but it can't get actions return value :(

Any ideas how to do something like that?

Regards, Daniel.

Upvotes: 1

Views: 1549

Answers (2)

Roman
Roman

Reputation: 13058

Try the class level respond_to

And something like this in your controller should do the trick:

def process_action(method_name, *args)
  self.response_body = send_action(method_name, *args).to_json
end

or maybe:

def process_action(method_name, *args)
  self.response_body = call(method_name, *args).to_json
end

Upvotes: 1

Jimmy
Jimmy

Reputation: 37081

Take a look at the respond_to/respond_with combo: http://railscasts.com/episodes/224-controllers-in-rails-3.

Upvotes: 0

Related Questions