2

I have a couple of links of my home page to different parts of my site that are restricted to those who are logged in.

If you are not logged in then you are redirected to the sign up form.

This isn't great however so i'd like to first of all redirect the user back to my home page. As well as display a flash message at the top of the screen informing them that they must be signed in first to proceed.

Can anyone help or point me in the right direction?

Keva161
  • 2,623
  • 9
  • 44
  • 68

2 Answers2

4

You can combine redirect_to and a flash message in the single statement:

redirect_to root_url, alert: "You're guest. That page was for users only :-)"
jdoe
  • 15,665
  • 2
  • 46
  • 48
0
#lib/custom_authentication_failure.rb
class CustomAuthenticationFailure < Devise::FailureApp 

  protected 

  def redirect_url 
    root_url
  end 
end

# /config/initializers/devise.rb
Devise.setup do |config|
...
  config.warden do |manager|
    manager.failure_app = CustomAuthenticationFailure
  end
end

If you’re getting an uninitialized constant CustomAuthenticationFailure error, and you’ve put the CustomAuthenticationFailure class under your /lib directory, make sure to autoload your lib files in your application.rb file:

config.autoload_paths += %W(#{config.root}/lib)

You have to be sure that the page where you redirect the user does not require authentication.

From http://adamtrepanier.net/post/7622315219/devise-authenticate-user-and-redirect and https://github.com/plataformatec/devise/wiki/_pages

edit: I assume that you are using authenticate_user!, to restrict access.

Mik
  • 4,117
  • 1
  • 25
  • 32
  • @Keva161, have you tried to restart the server? Also check out the class name that you declared, and which is added. – Mik May 30 '12 at 19:52