0

a little background story :

i want to put devise sign-in form on show method of my controller. it goes something like this

app/views/projects/show.html.erb

 <p><%= @project.name %> </p>
 <p><%= @project.description %> </p>
 <% if user_signed_in? %>
     // show link to download files
 <% else >
 <p> please login to view files </p>
     // show devise signin form
 <% end %>

and i want the user redirect to this current page (show view) whether sign-in are success or failed.

To address failed signin redirect - i've set custom_failure.rb in lib directory, set Devise initializer, and rails config.autoload_paths.

class CustomFailure < Devise::FailureApp
  def redirect_url
    url = Rails.application.routes.recognize_path(request.referer)
    if url[:controller] == "projects" && url[:action] == "show"
      stored_location_for(resource)
    else
      super
    end
   end

  def respond
    if http_auth?
      http_auth
    else
      redirect
   end
  end
end

my question is i do not want to use request.referer in this line in custom_failure.rb.

url = Rails.application.routes.recognize_path(request.referer)

since people says request.referer its not stable.

any ideas?

edit : actually my question is very similiar to this post here. in that post he wanted to redirect to site#index if sign in failed, while i want to redirect to current page if sign_in fails. rails (5.1.2) devise (4.4.1)

Aipack
  • 94
  • 9
  • I don't think that there is a solution for this. You can pass into form current url so that it can redirect back. – GorillaApe Mar 14 '18 at 07:46
  • did you read https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app ? – inye Mar 14 '18 at 13:11
  • @inye yes i did. that's how i get my form works if sign in process success. however the wiki doesn't cover how to handle redirection when sign in fails. by default if sign in fails devise will go to "users/sign_in" or similar. I need to it to stay on the current view and show the fail sign in message above. and i came with solution above which using request.referer (which i don't prefer to use) – Aipack Mar 14 '18 at 13:38
  • I found this http://codebeerstartups.com/2013/01/custom-redirect-after-login-fail-in-devise/ – inye Mar 14 '18 at 14:47

0 Answers0