DEV Community

Jeeho Lee
Jeeho Lee

Posted on

Redirect back, fallback location, and where it may fail

Redirect_back is a handy method that allows you to send back a user to the url that they accessed the action from. Fallback_location is a parameter in the method designed to provide a fallback URL in case the referrer is not available or cannot be determined. However, this is not a catch all for every situations, as it is very dependent on the request referrer.


A particular instance where I found it confusing that fallback didn't seem to be working was when I was destroying a record. For example, if we follow standard rails convention and tried to access a photo of id: 1 from a url called "website.com/photos/1", there would be a method defined in the controller as before_action where you might look to identify the photo id from params as such:

before_action :set_photo, only: %i[ show edit update destroy ]

...

def set_photo
   @photo = Photo.find(params[:id])
end
Enter fullscreen mode Exit fullscreen mode

Now, if I were to use a button in "website.com/photos/1" to destroy the corresponding photo record and specified a redirect back in the destroy action, an ActiveRecord::RecordNotFound error would come up even if the fallback_location is specified. This is because the referrer is recognized as "website.com/photos/1", and the before_action would try to run the set_photo method to identify params[:id], :id being 1 which no longer exists. As per api.rubyonrails.org:

"The referrer information is pulled from the HTTP Referer (sic) header on the request. This is an optional header and its presence on the request is subject to browser security settings and user preferences. If the request is missing this header, the fallback_location will be used."

In our particular situation, this request header does exist (likely website.com/photos/1), albeit pointing to a record that no longer exists. Thus, the fallback is not considered since we do have a referrer, and we're instead met with the RecordNotFound error.

To deal with this RecordNotFound error, we could choose to add in a rescue for the RecordNotFound in a before_action to redirect to our fallback url, or alternatively conditionally check what our referrer is to situationally apply redirect_back. A simple example addition to our situation above could be to add the rescue into our set_photo method like this:

before_action :set_photo, only: %i[ show edit update destroy ]

...

def set_photo
   @photo = Photo.find(params[:id])
   rescue ActiveRecord::RecordNotFound => _
      redirect_to root_url
end
Enter fullscreen mode Exit fullscreen mode

The point to clarify though is that there are situations like this where fallback location might not work like we'd want. Fallback location is still a great tool for situations like when a user tries to directly access an action by typing in a url, where the request referrer would be nil. However, it's important to know when/where these tools actually work and when/where they are not considered.

Top comments (0)