App not creating sessions and @current_user not working
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have some code here that is supposed to create a session upon login, and create "@current_user". However, neither of these things are working, and after 2 days of speculating, I can't figure out why.
Users controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to login_path
flash[:success] = "Account created. You may now log in"
else
redirect_to '/signup'
flash[:warning] = "Something went wrong. Try again."
end
end
private
def user_params
params.require(:user).permit(:username, :jabber_id, :password)
end
end
Sessions controller:
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by_username(params[:session][:name])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/posts'
else
session[:user_id] = nil
flash[:warning] = "Failed login- try again"
redirect_to '/login'
end
end
def destroy
session[:session_id] = nil
redirect_to login_path
end
end
Application controller:
class ApplicationController < ActionController::Base
def current_user
return unless session[:user_id]
@current_user ||= User.find(session[:user_id])
end
def require_user
redirect_to '/login' unless current_user
end
end
ruby-on-rails ruby
|
show 4 more comments
I have some code here that is supposed to create a session upon login, and create "@current_user". However, neither of these things are working, and after 2 days of speculating, I can't figure out why.
Users controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to login_path
flash[:success] = "Account created. You may now log in"
else
redirect_to '/signup'
flash[:warning] = "Something went wrong. Try again."
end
end
private
def user_params
params.require(:user).permit(:username, :jabber_id, :password)
end
end
Sessions controller:
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by_username(params[:session][:name])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/posts'
else
session[:user_id] = nil
flash[:warning] = "Failed login- try again"
redirect_to '/login'
end
end
def destroy
session[:session_id] = nil
redirect_to login_path
end
end
Application controller:
class ApplicationController < ActionController::Base
def current_user
return unless session[:user_id]
@current_user ||= User.find(session[:user_id])
end
def require_user
redirect_to '/login' unless current_user
end
end
ruby-on-rails ruby
1
have you tried debugging atsession[:user_id] = @user.id
to see if that code was actually hit?
– Lenin Raj Rajasekaran
Jan 4 at 0:53
1
what happens? tell us more
– Ursus
Jan 4 at 1:00
1
So you get the "failed login" message? Do you havehas_secure_password
in yourUser
model, and thebcrypt
gem is installed?
– Drenmi
Jan 4 at 2:40
1
No, the login doesn't fail, but the session creation does.
– yhtr
Jan 4 at 2:41
1
For your second question, yes on both counts.
– yhtr
Jan 4 at 2:41
|
show 4 more comments
I have some code here that is supposed to create a session upon login, and create "@current_user". However, neither of these things are working, and after 2 days of speculating, I can't figure out why.
Users controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to login_path
flash[:success] = "Account created. You may now log in"
else
redirect_to '/signup'
flash[:warning] = "Something went wrong. Try again."
end
end
private
def user_params
params.require(:user).permit(:username, :jabber_id, :password)
end
end
Sessions controller:
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by_username(params[:session][:name])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/posts'
else
session[:user_id] = nil
flash[:warning] = "Failed login- try again"
redirect_to '/login'
end
end
def destroy
session[:session_id] = nil
redirect_to login_path
end
end
Application controller:
class ApplicationController < ActionController::Base
def current_user
return unless session[:user_id]
@current_user ||= User.find(session[:user_id])
end
def require_user
redirect_to '/login' unless current_user
end
end
ruby-on-rails ruby
I have some code here that is supposed to create a session upon login, and create "@current_user". However, neither of these things are working, and after 2 days of speculating, I can't figure out why.
Users controller:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to login_path
flash[:success] = "Account created. You may now log in"
else
redirect_to '/signup'
flash[:warning] = "Something went wrong. Try again."
end
end
private
def user_params
params.require(:user).permit(:username, :jabber_id, :password)
end
end
Sessions controller:
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by_username(params[:session][:name])
if @user && @user.authenticate(params[:session][:password])
session[:user_id] = @user.id
redirect_to '/posts'
else
session[:user_id] = nil
flash[:warning] = "Failed login- try again"
redirect_to '/login'
end
end
def destroy
session[:session_id] = nil
redirect_to login_path
end
end
Application controller:
class ApplicationController < ActionController::Base
def current_user
return unless session[:user_id]
@current_user ||= User.find(session[:user_id])
end
def require_user
redirect_to '/login' unless current_user
end
end
ruby-on-rails ruby
ruby-on-rails ruby
edited Jan 4 at 9:43
SRack
4,55341738
4,55341738
asked Jan 4 at 0:24
yhtryhtr
63
63
1
have you tried debugging atsession[:user_id] = @user.id
to see if that code was actually hit?
– Lenin Raj Rajasekaran
Jan 4 at 0:53
1
what happens? tell us more
– Ursus
Jan 4 at 1:00
1
So you get the "failed login" message? Do you havehas_secure_password
in yourUser
model, and thebcrypt
gem is installed?
– Drenmi
Jan 4 at 2:40
1
No, the login doesn't fail, but the session creation does.
– yhtr
Jan 4 at 2:41
1
For your second question, yes on both counts.
– yhtr
Jan 4 at 2:41
|
show 4 more comments
1
have you tried debugging atsession[:user_id] = @user.id
to see if that code was actually hit?
– Lenin Raj Rajasekaran
Jan 4 at 0:53
1
what happens? tell us more
– Ursus
Jan 4 at 1:00
1
So you get the "failed login" message? Do you havehas_secure_password
in yourUser
model, and thebcrypt
gem is installed?
– Drenmi
Jan 4 at 2:40
1
No, the login doesn't fail, but the session creation does.
– yhtr
Jan 4 at 2:41
1
For your second question, yes on both counts.
– yhtr
Jan 4 at 2:41
1
1
have you tried debugging at
session[:user_id] = @user.id
to see if that code was actually hit?– Lenin Raj Rajasekaran
Jan 4 at 0:53
have you tried debugging at
session[:user_id] = @user.id
to see if that code was actually hit?– Lenin Raj Rajasekaran
Jan 4 at 0:53
1
1
what happens? tell us more
– Ursus
Jan 4 at 1:00
what happens? tell us more
– Ursus
Jan 4 at 1:00
1
1
So you get the "failed login" message? Do you have
has_secure_password
in your User
model, and the bcrypt
gem is installed?– Drenmi
Jan 4 at 2:40
So you get the "failed login" message? Do you have
has_secure_password
in your User
model, and the bcrypt
gem is installed?– Drenmi
Jan 4 at 2:40
1
1
No, the login doesn't fail, but the session creation does.
– yhtr
Jan 4 at 2:41
No, the login doesn't fail, but the session creation does.
– yhtr
Jan 4 at 2:41
1
1
For your second question, yes on both counts.
– yhtr
Jan 4 at 2:41
For your second question, yes on both counts.
– yhtr
Jan 4 at 2:41
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031636%2fapp-not-creating-sessions-and-current-user-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54031636%2fapp-not-creating-sessions-and-current-user-not-working%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
have you tried debugging at
session[:user_id] = @user.id
to see if that code was actually hit?– Lenin Raj Rajasekaran
Jan 4 at 0:53
1
what happens? tell us more
– Ursus
Jan 4 at 1:00
1
So you get the "failed login" message? Do you have
has_secure_password
in yourUser
model, and thebcrypt
gem is installed?– Drenmi
Jan 4 at 2:40
1
No, the login doesn't fail, but the session creation does.
– yhtr
Jan 4 at 2:41
1
For your second question, yes on both counts.
– yhtr
Jan 4 at 2:41