문제

I'd like a controller method to respond by disabling/denying layout if the request happens to be an ajax request (i.e., request.xhr? == true).

The following doesn't seem to work -- it returns the layout nonetheless

class FooController < Ramaze::Controller
  layout :default

  def bar
    if request.xhr?
      layout nil
      "return something here"      #just return this string without the layout
    else
      ... #return with full layout
   end
end
도움이 되었습니까?

해결책

You can bypass view and layout rendering by calling respond! like this :

respond!(body, status, 'Content-Type' => 'whatever/foo')

Where body is your (json ?) body, status the HTTP response code. You can then provide a hash with returned headers.

However, you might be interested in provides that can handle requests differently if they end, for instance, with '.json'. See http://ramaze.net/documentation/file.views.html#View_Mapping for more info on this.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top