ruby on rails - Application Controller level validation with redirect -
I'm sure, make sure that the best way to make some necessary properties of each user is not what And if they don 't want me to' new 'page like The above example is flawed because it creates a redirect loop. I can definitely use some advice about making this kind of recognition thanks
class ApplicationController & lt to redirect them; ActionController :: Base protect_from_forgery before_filter: authenticate_user!,: Valid_location def valid_location if (? Current_user.location.nil || current_user.location.city.nil) redirect_to new_user_locations_path (current_user.id) and true end end
cause that this is creating a loop, because valid_location
method is also controller be called new_user_locations_path . To prevent this you need to make sure that the filter does not run with the controller (
skip_before_action
in Rail 4). There is such a problem.
Classroom Controller & lt; ApplicationController skip_before_filter: valid_location, only [New ,: Create] # ... end
because valid_location
provides a real / false Boolean, I rename I recommend valid_location? Referring method and reasoning to
or invalid_location
:
class ApplicationController & lt; ActionController :: Base protection_from_forgery before_filter: authenticate_user!,: Redirect_invalid_locations private def redirect_invalid_locations redirect_to (new_user_locations_path (current_user)) that invalid_location? End def invalid_location? Current_user.try (: location) .try (: city) .nil? End and Class Location Controller & lt; ApplicationController skip_before_filter: redirect_invalid_locations, only: [: new,: create] expiration
Comments
Post a Comment