Different code in same controller action for html or json

actioncontroller, ruby-on-rails

Solution

May be something like this will work for you.

def index
  respond_to |format|
     format.html { index_html}
     format.json { index_json }
  end
end

def index_html
  ...
end
def index_json
  ...
end

Problem

I have a controller action that responds to the same root in two formats - html and json. But the code that runs for the html response is completely different than the one for the json response.. Now I have something like ``` def index result_html = ... result_json = ... respond_to |format| format.html format.json { result = result_json.limit(10) } end end ``` and I would like to have it like ``` def index.html result_html ... end ``` and ``` def index.json result_json ... end ``` What would be the best way to organize it?

Original source