[英]logged_in_user not working bypassing landing page for logged in users
I've seen a few similar posts about this but they didn't seem to resolve my problem. 我已经看到了一些与此类似的帖子,但是它们似乎并不能解决我的问题。
Problem It simply doesn't work. 问题它根本不起作用。 Doesn't error out, nothing, just doesn't evaluate and I don't know why because it does for other things?
不会出错,什么都不会,只是不进行评估,我也不知道为什么,因为它对其他事情有用吗?
Here is my Welcome controller definition: 这是我的Welcome控制器定义:
def show
if logged_in?
# raise #I raise an error here and nothing happens on the web page. Just to confirm this isn't working for my sanity.
redirect_to recipes_url
else
render template: "welcome/#{params[:welcome]}"
end
end
The function logged_in? 函数login_in? is defined in my /helpers/sessions_helper.rb:
在我的/helpers/sessions_helper.rb中定义:
def logged_in?
!current_user.nil?
end
Here is the current_user: 这是current_user:
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
# raise #The test still pass, so this branch is currently untested.
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
And my routes are: 我的路线是:
get 'welcome/:welcome' => 'welcome#show'
root :to => 'welcome#home'
Why doesn't this work? 为什么不起作用? It just stays on the welcome page regardless of if I'm logged in or not.
无论我是否登录,它都停留在欢迎页面上。 I know that logged_in?
我知道login_in吗? is evaluating correctly because I have other links on the page that are dynamic based upon this.
之所以正确评估,是因为页面上还有其他基于此而动态的链接。
EDIT: What's really weird is that the logged_in? 编辑:真正奇怪的是login_in吗? works in my view just not in this controller.
在我看来,仅在此控制器中有效。
Here's a snippet from the nav of in the application layout view. 这是应用程序布局视图中的导航片段。
<% if logged_in? %>
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Log out", logout_path, method: "delete" %></li>
<% else %>
<li><%= link_to "Browse recipes", recipes_path %></li>
<li><%= link_to "Log in", login_path %></li>
<% end %>
logged_in_user
isn't actually doing or returning anything if the user is already signed in. It just returns nil which evaluates to false, so there's no way for your show
action to even reach the redirect_to recipes_url
statement. 如果用户已经登录,
logged_in_user
实际上并没有执行任何操作或不返回任何内容。它仅返回nil(其值为false),因此您的show
操作甚至无法到达redirect_to recipes_url
语句。
Did you intend to use the logged_in?
您打算使用
logged_in?
function? 功能? This should work, provided
logged_in?
提供
logged_in?
,这应该可以工作logged_in?
does what its name implies: 顾名思义就是:
def show
if logged_in?
redirect_to recipes_url
else
render template: "welcome/#{params[:welcome]}"
end
end
If using devise, make sure you're using a model named User
, have devise defined in your routes.rb
with devise_for :users
and have devise declared on your model with at least the following: 如果使用devise,请确保使用名为
User
的模型,并在routes.rb
使用devise_for :users
定义了routes.rb
,并在模型上声明了devise_for :users
,至少包含以下内容:
devise :database_authenticatable,
:registerable,
:rememberable,
:validatable
Ok. 好。 The logged_in method was working fine.
logging_in方法工作正常。 The reason that this wasn't working was because my route was going directly to the view 'welcome#home' and bypassing the controller altogether.
之所以不起作用,是因为我的路线是直接转到视图“ welcome#home”,并完全绕开了控制器。
As soon as I made the route 'welcome#show' I was able to get this to work. 一旦我将路线设置为“ welcome#show”,便可以使它正常工作。
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.