Reputation: 786
I'm trying to learn RoR by developing my own custom CMS (yeah, yeah...) and I'm wanting to do a simple thing, but am unable to come up with a clean solution.
My routes file has the following match:
match '/login' => "sessions#new"
What it does is, upon hitting "http://localhost:3000/login", is let me login to create a new session. It works perfectly...
However, this is what my Sessions controller looks like:
class SessionsController < ApplicationController
def create
@username = params[:session][:username]
@password = params[:session][:password]
@rUser = User.find_by_username(params[:session][:username])
#if user && user.authenticate(params[:session][:password])
if @rUser && Digest::SHA2.hexdigest(@password) == @rUser.password
# Sign the user in and redirect to the user's show page.
redirect_to users_path, :flash => { :notice => "Logged in as #{@username}." }
else
# Create an error message and re-render the signin form.
redirect_to '/login', :flash => { :error => "Login failed. Either the username or password was incorrect." }
end
end
end
If you take a look at the "else" statement in my if-else conditional, you'll see that I'm using redirect_to '/login' to display the same form on login fail. Is there any better way to create a permanent link to '/login' rather than having to use a string, because if I decide to change the URL in the future, I'd prefer not to have to make the change in multiple places.
Sorry if it's not clear! Feel free to ask for further code (not showing in fear of pathetic-ness of it).
Upvotes: 0
Views: 184
Reputation: 10885
You can do like this
match '/login' => "sessions#new", :as => :login
#if want to call like localhost:3000/login
match '/sign_in' => "sessions#new", :as => :sign_in
#if want to call like localhost:3000/sign_in
Upvotes: 2
Reputation: 15788
match '/login' => "sessions#new", :as => :login
Then you can use login_path
in your controllers.
Upvotes: 4