Skip to content

Instantly share code, notes, and snippets.

@juanm55
Created February 18, 2012 00:18
Show Gist options
  • Save juanm55/1856452 to your computer and use it in GitHub Desktop.
Save juanm55/1856452 to your computer and use it in GitHub Desktop.
something I did to avoid using 2 root_paths in my routes
def home
# get 'login', :to => 'devise/sessions#new' in my routes.rb
redirect_to 'login' unless current_user #if not logged in, kick him out of here!
end
@juanm55
Copy link
Author

juanm55 commented Feb 18, 2012

it may not be the most elegant solution... but, it works, and it does not mess too much with RESTfulness, or fatten my controller... so.....

and I didn't create a new actions to handle redirection because, anyway, the only thing that #home shows is a menu to where you can go...

@krainboltgreene
Copy link

Devies (ugh) should actually have a "require_login" action, so you can do this:

class ThingController < ApplicationController
  before_filter :require_login, only: [:home]
end

At least, sorcery does (which is a better library anyhow).

This will redirect to the action setup by the library, if the user isn't logged in.

I'll also note two ruby idioms that you should use:

# Single line if
action if condition

# or
redirect_to 'login' if !current_user

As well as the more clear way to write ! type conditions:

# The not modifier
redirect_to 'login' if not current_user

# The unless keyword
redirect_to 'login' unless current_user

@juanm55
Copy link
Author

juanm55 commented Feb 18, 2012

  • regarding the first block: yes Devise does have something similar
class ThingController < ApplicationController
  before_filter :authenticate_user!
end

which would enforce authentication for all of the actions in the controller and redirect to the action I tell it to,,, and it can be passed regular, :only and :except

  • sorcery ... haven't used it yet,,, just whipped through the docs and the railscasts (ofcourse) and seems pretty simple to use, and the fact that you get to write the controllers and all does make one think a thing or two, and learn in the process,
    I'll consider it for a next project! thanks,,, plus it has a "Very active" (at ruby toolbox) development activity too, so I definitely have to check it out
    I really like that if I write the controllers and the logic, I know it, and I know what is being done,,,, with Devise all that logic (the code itself) is hidden (guess for security reasons...)
  • regarding the idioms: THANK YOU so much.... you are completely right in the use of not, it is more readable
    the single line if I have used it,,, simply didn't remember it on place,,,, and I also like the ternary operator (i saw it on js) and I know there is something similar in ruby but haven't looked up the syntax yet
    about this same topic, I have been reading the ruby documentation lately and the rails best practices web, but maybe those aren't the places to find this kind of advices/tricks, if you got any resource that can lead me "out of the dark side" I would greatly appreciate those

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment