Question

I have a controller that deals with contact emails. I have an action like this:

def representative
    @contact = UserContact.new()
    if request.method == 'POST'
     @contact = UserContact.new(params[:merchant_contact])
     if @contact.valid?
      @contact.contact_email = current_user.email
      @contact.contact_phone = current_user.phone_number
      Emailer.representative_contact(@contact, representative).deliver # sends the email
          redirect_to **????**, :flash => { :notice => "Thank you! Someone will get back to you shortly."}
     else
      render :representative
     end
    else
     render :representative
    end
end

I call contact_representative_path from different places in my code, and I want that, after submission a redirection to where the user clicked the contact_representative_path. I have tried with :back, but it just renders the representative view.

Was it helpful?

Solution

redirect_to and return does not end the requests, in order to end the request you need to add return at the end.

Try with redirect_to contact_representative_path,:flash => { :notice => "Thank you! Someone will get back to you shortly."} and return

UPDATE

In session hash, you can store the requesting url which will be available across multiple requests. session[:return_to] ||= request.referer Then call redirect_to session[:return_to], :flash => { :notice => "Thank you! Someone will get back to you shortly."}

Check this question, hope this will solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top