samedi 18 mars 2017

rails check if url ends with number

I am trying to create a redirect_to :back that is triggered after a person object is deleted. There are several pages a user can come from before triggering the delete object code. If they come from the person#show page, i want to redirect to a route called admin_persons_path. If they come from any other page, I just want the user to be redirected back to the page they are on. The persons#show page has a url structure of localhost:3000/admin/persons/[some_id_number] . I am trying to write code that reads the referrer url and redirects accordingly. I am trying to do something like:

if request.referer.end_with?(some regular expression)
    redirect_to :back
else
    redirect_to admin_persons_path
end

The problem is that I dont think the end_with? method can accept a regular expression because when I try it with a regex i get the error:

TypeError: no implicit conversion of Regexp into String

How do I get this to work?

Please note using redirect_back(fallback_location: admin_persons_path) won't work because the referrer technically exists. If there is a built-in method that accomplishes what I want, I am unaware of it.

Aucun commentaire:

Enregistrer un commentaire