Home>
■ I don't know
Users who are not signed in before_aciton skip to the sign-in screen, and users who are signed in have set helper and before_action to skip to the index screen, but they can jump to other pages without signing in The
I would like to know the cause.
■ users_helper.rb
module UsersHelper
def user_sign_in (user)
session [: @ user_id] = user.id
end
def current_user
if @ current_user.nil?
@current_user = User.find_by (id: session [: @ user_id])
else
@current_user
end
end
def user_sign_out
session.delete (: @ user_id)
@current_user = nil
end
def user_signed_in?
@ current_user.present?
end
def authorize
redirect_to sign_in_path unless user_signed_in?
end
def redirect_to_index_if_signed_in
redirect_to clients_index_path and return if user_signed_in?
end
end
■ users_controllers.rb
include UsersHelper
before_action: authorize, except: [: sign_in,: sign_in_process]
before_action: redirect_to_index_if_signed_in, only: [: sign_in]
def sign_in
@user = User.new
render layout: "application_not_login"
end
def sign_in_process
@user = User.find_by (email: user_params [: email])
if @user&&@ user.authenticate (user_params [: password])
user_sign_in (@user)
redirect_to clients_index_path and return
else
flash [: danger] = "Sign-in failed"
render "sign_in"
end
end
■ routes
get '/', to: 'users # sign_in', as:: sign_in
get 'clients', to: 'clients # index', as:: clients_index
-
Answer # 1
Related articles
- ruby - rails page does not open
- ruby on rails - validation does not work
- ruby on rails - api key is not read by dotenv-rails
- ruby - validation does not start
- ruby on rails - not created
- ruby version does not change
- ruby on rails - docker file is not up
- ruby on rails 5 - how to check the value of before_action with debugger
- ruby - validation does not work
- ruby on rails - no route matches , missing required keys: [: id] cannot be resolved
- ruby on rails - i want to solve the problem that scss is not applied after modifying the scss file with rails
- ruby on rails - about the legitimacy of db design
- [sqlite, ruby, rails] i want to sort the hash value data obtained from the table as i expected
- ruby - i want to collect the same records using rails group and get the value
- ruby - [rails] how to resolve activerecord errors
- ruby - allow rails chat app to save to only one room
- ruby on rails - [rails] i want to solve "no route matches [post]"/messages/search "" of "routing error&
- ruby on rails - i get an error in a program that changes over time
- ruby - data is not updated by update action rails
- ruby - use form_with in rails to specify multiple models and save values
Trends
I used the wrong controller to specify before_action