开发者

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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜