hello guys
i have a problem
in ApplicationController i have
before_action :login_required
def login_required
redirect_to login_path, alert: 'Write ur email & password' unless current_user
end
def current_user
@current_user ||=User.find session[:user_id] if session[:user_id]
end
i try to skip_before_action in UsersController so that i can create a user
in UsersController
skip_before_action :login_required, only: [:new, :create]
def create
permitted = params.require(:user).permit(:email, :password, :password_confirmation)
@user = User.new permitted
if @user.save
redirect_to tasks_path
else
render :new
end
and when i try to redirect tasks path i get
"Started GET "/tasks" for 127.0.0.1 at 2015-11-30 16:34:52 +0300
Processing by TasksController#index as HTML
Redirected to http://localhost:3000/login
Filter chain halted as :login_required rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)"
How i can fix it?
···
--
maxim hedrovich
before_action :login_required
...
skip_before_action :login_required, only: [:new, :create]
...
and when i try to redirect tasks path i get
...
Filter chain halted as :login_required rendered or redirected
I ass-u-me the actual problem is that your screen winds up at the
registration again, rather than the new user's task list, right?
You've only *created* that user, you haven't *logged him in* (as far
as you've shown, anyway). Do that and the code looks to me like it
ought to work.
-Dave
···
On Mon, Nov 30, 2015 at 9:27 AM, maxim hedrovich <hedrovich@list.ru> wrote:
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.
Sounds like the right behavior according to your code. It seems like you're not logged in when you create a user, when the user is saved you are redirected to "tasks_path" which is protected by "login_required" giving you the error you are seeing. You need to be logged in to see "/tasks". You're missing logging them in so it could be as simple as:
if @user.save
session[:user_id] = @user.id
redirect_to tasks_path
That would create the user, immediately "logged them in", and redirect them to "/tasks". Not sure if that's your intention.
Best,
Jorge Colon
Senior Software Architect/Technical Advisor/Owner
2UP Media
c: 407.489.2677
jorge@2upmedia.com
www.2upmedia.com
"I help refurbish legacy PHP custom software to get another 18 months out of them"
LinkedIn
PHP Zend Certified Engineer
···
On Nov 30, 2015, at 9:27 AM, maxim hedrovich <hedrovich@list.ru> wrote:
hello guys
i have a problem
in ApplicationController i have
before_action :login_required
def login_required
redirect_to login_path, alert: 'Write ur email & password' unless current_user
end
def current_user
@current_user ||=User.find session[:user_id] if session[:user_id]
end
i try to skip_before_action in UsersController so that i can create a user
in UsersController
skip_before_action :login_required, only: [:new, :create]
def create
permitted = params.require(:user).permit(:email, :password, :password_confirmation)
@user = User.new permitted
if @user.save
redirect_to tasks_path
else
render :new
end
and when i try to redirect tasks path i get
"Started GET "/tasks" for 127.0.0.1 at 2015-11-30 16:34:52 +0300
Processing by TasksController#index as HTML
Redirected to http://localhost:3000/login
Filter chain halted as :login_required rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)"
How i can fix it?
--
maxim hedrovich
Hi Maxim,
It’s a rails issue, not really ruby.
"skip_before_action :login_required, only: [:new, :create]” skip the login_required only for actions :new and :create. I think you want to make:
skip_before_action :login_required, only: :index
It will skip the login_required method only for :index action
bests,
Seb
···
On Nov 30, 2015, at 3:27 PM, maxim hedrovich <hedrovich@list.ru> wrote:
hello guys
i have a problem
in ApplicationController i have
before_action :login_required
def login_required
redirect_to login_path, alert: 'Write ur email & password' unless current_user
end
def current_user
@current_user ||=User.find session[:user_id] if session[:user_id]
end
i try to skip_before_action in UsersController so that i can create a user
in UsersController
skip_before_action :login_required, only: [:new, :create]
def create
permitted = params.require(:user).permit(:email, :password, :password_confirmation)
@user = User.new permitted
if @user.save
redirect_to tasks_path
else
render :new
end
and when i try to redirect tasks path i get
"Started GET "/tasks" for 127.0.0.1 at 2015-11-30 16:34:52 +0300
Processing by TasksController#index as HTML
Redirected to http://localhost:3000/login
Filter chain halted as :login_required rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)"
How i can fix it?
--
maxim hedrovich
Sorry, i’ve misread your mail, Dave is right, after the user :create you need to sign in manually your users.
Seb
···
On Nov 30, 2015, at 3:47 PM, Dave Aronson <ruby-talk.list.2.TRex@codosaur.us> wrote:
On Mon, Nov 30, 2015 at 9:27 AM, maxim hedrovich <hedrovich@list.ru> wrote:
before_action :login_required
...
skip_before_action :login_required, only: [:new, :create]
...
and when i try to redirect tasks path i get
...
Filter chain halted as :login_required rendered or redirected
I ass-u-me the actual problem is that your screen winds up at the
registration again, rather than the new user's task list, right?
You've only *created* that user, you haven't *logged him in* (as far
as you've shown, anyway). Do that and the code looks to me like it
ought to work.
-Dave
--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.