Using response_to for graceful degradation using ajax in RoR 2.x

I was looking at an AWDR book on web development with rubies on rails, and one of the problems with the old code was that it did not use response_to to make sure that the view used would be a javascript view. Now, in some updated examples, I saw people mention them later, when applying graceful degradation, use request.xhr? to indicate if javascript is enabled and if they do not redirect the user.

I was wondering if you can use response_to to get the same behavior, and if so, if it is considered good shape or not, and why?

So what I'm thinking of doing something like:

def function
  respond_to do |format|
    format.js do
      basic_stuff
    end
    format.html do
      basic_stuff
      user_redirect
    end
  end
end

, sorta DRY, , , - , . , API .

+3
1

:

def function
  basic_stuff # executed regardless of the mime types accepted
  respond_to do |format|
    format.html do
      user_redirect
    end
  end
  # will fall back rendering the default view - which you should ensure will be js
end

request.xhr? X-Requested-With ( , XMLHttpRequest). respond_to mime.

.

. xhr? , ajax (Prototype ).

, respond_to , xml, json, js, .

respond_to .

+2

Source: https://habr.com/ru/post/1697679/


All Articles