简体   繁体   中英

Rails: using respond_to without responding

For a Ruby on Rails project, i have to implement two different methods to check if the sender of a request has the rights to access, depending on the "Accept"- header. I located the check in the Application controller and use before_action to call it from different controllers. Knowing that Rails has a way to treat different "Accept"-types, I want do do the following:

class ApplicationController < ActionController::Base

#my function to test, if the sender can access
  def authenticate
    respond_to do |format|
      format.json {
        #do the test for an API request
      }
      format.html {
        #do the test for a HTML website request
      }
    end
  end
end

Since my respond_to won't always call a redirect_to or render (if the sender has access, another controller method will be executed), I'm wondering, if I can still use it like this. Or should I analyse the headers instead?

The point of respond_to is to do different things based on the type of request (HTML, JS, JSON, etc.): therefore, it doesn't matter if you need redirect_to or render.

If you have to change the behavior of your method based on which type of request you receive, feel free to use it; if the behavior would be the same regardless, respond_to is unnecessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM