2

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?

fidato
  • 719
  • 5
  • 22
Yo Ludke
  • 2,149
  • 2
  • 23
  • 38

2 Answers2

5

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
Pavel S
  • 1,543
  • 8
  • 14
1

You can test for the format with request.format.symbol then when :json call your json action or when :html call your html action.

Jaffa
  • 12,442
  • 4
  • 49
  • 101