Home > Net >  Redirect to diferent paths depending from where do you acces a form
Redirect to diferent paths depending from where do you acces a form

Time:06-28

I'm using the same form in two different sites of the application and depending from where I access to the form I want to be redirected to different paths after submitting it.

This is what I have in my controller:

def create
 @item = Offer.new(offer_params)

 authorize @item

 if @item.save
  condition_I_don't_know_how_to_write ? redirect_to(x_path) : redirect_to(y_path) 
 else
  render :new
 end
end

CodePudding user response:

You can find out the URL where the request originated with request.referrer. It will give you the whole URL in the format https://example.com/products/1. You can then quite easily extract the information you need from that URL, for example the path with URI(request.referrer).path, which would in this example return /products/1.

Another option would be to use nested routes. This way you could have your request.path be something like /products/1/offers/new, which is less ambiguous and avoids the hassle of getting the request.referrer and then extracting the path from that.

  • Related