我创建了两种类型的用户 - "用户"和"商店"。我的应用程序已设置好,当用户登录时,会将其发送到主页,然后通过ruby if-elsif-else逻辑显示其配置文件。
我的问题是,逻辑的第二部分elsif无法正常工作,而是呈现主页而非商店资料页面。
home.html.erb
<% if logged_in? %> // corresponds to "user" model
<div>
<aside class="col-md-4">
<div>
<h1>My Profile</h1>
</div>
<div class="hero-unit user_info">
<%= render 'shared/user_info' %>
</div>
<section class="hero-unit car_form">
<%= render 'shared/car_form' %>
</section>
</aside>
<div class="col-md-8">
<h1>My Cars</h1>
</div>
<div class="col-md-8">
<%= render 'shared/feed' %>
</div>
</div>
<% elsif shop_logged_in? %> // checks if a shop has logged in
<div class="col-md-8">
<%= render 'shared/shop_info' %>
</div>
<% else %> //If no one is logged in, home page is shown
<div class="broke col-md-12">
<div class="alive">
<h1>Keep your car on the road<%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %></h1>
</div>
</div>
我需要让<% elsif shop_logged_in? %>
部分正常工作。这在ShopSessionHelper中定义:
def shop_logged_in?
!current_shop.nil?
end
然后是SessionHelper,用于用户/驱动程序类。我有点担心两者之间有些重叠,但似乎工作正常(欢迎提出建议)。
def logged_in?
!current_user.nil?
end
这两个应该告诉用户或商店是否已登录,并使用上述逻辑呈现正确的页面。为什么它不起作用?
为了彻底,这里有我的控制器和帮助者:
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to root_path
else
#Show error message
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
sessions_helper.rb
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
def current_user?(user)
user == current_user
end
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end
# Redirects to stored location (or to the default).
def redirect_back_or(default)
redirect_to(root_url)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
shop_sessions_controller.rb
class ShopSessionsController < ApplicationController
def new
end
def create
shop = Shop.find_by(email: params[:session][:email].downcase)
if shop && shop.authenticate(params[:session][:password])
log_in shop
params[:session][:remember_me] == '1' ? remember(shop) : forget(shop)
redirect_to root_path
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
shop_sessions_helper.rb
module ShopSessionsHelper
# Logs in the given shop.
def log_in(shop)
session[:shop_id] = shop.id
end
# Remembers a shop in a persistent session.
def remember(shop)
shop.remember
cookies.permanent.signed[:shop_id] = shop.id
cookies.permanent[:remember_token] = shop.remember_token
end
def current_shop?(shop)
shop == current_shop
end
# Returns the shop corresponding to the remember token cookie.
def current_shop
if (shop_id = session[:shop_id])
@current_shop ||= Shop.find_by(id: shop_id)
elsif (shop_id = cookies.signed[:shop_id])
shop = Shop.find_by(id: shop_id)
if shop && shop.authenticated?(cookies[:remember_token])
log_in shop
@current_shop = shop
end
end
end
# Returns true if the shop is logged in, false otherwise.
def shop_logged_in?
!current_shop.nil?
end
def forget(shop)
shop.forget
cookies.delete(:shop_id)
cookies.delete(:remember_token)
end
# Logs out the current shop.
def log_out
forget(current_shop)
session.delete(:shop_id)
@current_shop = nil
end
# Redirects to stored location (or to the default).
def redirect_back_or(default)
redirect_to(root_url)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
答案 0 :(得分:0)
<% if logged_in? %> // corresponds to "user" model
<% elsif shop_logged_in? %> // corresponds to "user" model
只检查一个或另一个,并且无法同时处理两者。因此,为了简单起见,我将取消商店会话控制器,并在单个会话控制器中处理商店会话和用户会话。
为了处理在用户根据您的评论登录时不允许商店登录,我会这样做:
在Shop Sessions Helper中将def log_out
修改为def shop_log_out
,并将其添加到用户会话帮助中的log_in
:
商店会议助手:
def shop_log_out
#your same code from before
end
用户会话帮助:
def log_in(user)
session[:user_id] = user.id
if shop_logged_in? #check if a shop is logged in
shop_log_out #and log shop out if true
end
end
现在,如果有商店登录,只要用户登录就会注销,防止两者同时发生。
如果您选择忽略我关于仅使用一个会话控制器的建议,我会这样做:
<% elsif shop_logged_in? %> // checks if a shop has logged in
失败了。尝试使用puts
追溯您的方法,找出失败的位置。我会一次退回一个方法,从:
def shop_logged_in?
puts "-------------------------" #add this code
puts "current_shop = #{current_shop}" #this code
puts "-------------------------" #and this code
!current_shop.nil?
end
然后保存代码并导航到您的主页。 你很可能会得到一个错误,说明有关current_shop的内容为零。
现在从puts
中删除shop_logged_in?
,然后转到def current_shop
:
def current_shop
if (shop_id = session[:shop_id])
puts "---------------------" #add this
puts "inside current_shop if" #and this
@current_shop ||= Shop.find_by(id: shop_id)
elsif (shop_id = cookies.signed[:shop_id])
shop = Shop.find_by(id: shop_id)
if shop && shop.authenticated?(cookies[:remember_token])
log_in shop
@current_shop = shop
end
end
end
如果没有任何内容包含上面修改过的代码,那么我会将puts
向下移动到elsif
以找出您正在流经的逻辑部分。如果puts
都没有,那么您知道if
和elsif
def current_shop
内的routes.rb
评估为真。
如果是这种情况,那么我会说你的shopsession#new
文件是错误的。您的商店是否登录路径include ShopSessionsHelper
?
我能想到的另一件事是忘记在Application Controller中添加{{1}}。