在 rails 中调用应用程序控制器中的当前用户
Call the current user in application controller in rails
大家好,我是 rails 的新手,如果我的问题非常简单或愚蠢,请理解我的意思。我目前正在使用 devise
gem 并且我试图将模态放入我的 application.html.erb
以便模态始终出现在顶部但我收到的错误消息是
undefined local variable or method `current_user' for ApplicationController:Class
要在Application.html.erb
中调用Post.rb
中的记录,我应该在哪里修复?感谢您的帮助!
Application.html.erb
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li><%=link_to 'Sign out', destroy_user_session_path, method: 'delete' %></li>
<%=render "post_list", post_list: @post_list%> //I tried to call this
<% else %>
<li><%=link_to 'Sign in', new_user_session_path %></li>
<li><%=link_to 'Sign up', new_user_registration_path %></li>
<% end %>
</ul>
应用程序控制器
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
@post_list = Post.where(user_id: current_user).list?
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
您可以在 ApplicationController
中访问 current_user
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def current_user_posts?
@post_list = Post.where(user_id: current_user).list? // current_user should work here.
end
end
大家好,我是 rails 的新手,如果我的问题非常简单或愚蠢,请理解我的意思。我目前正在使用 devise
gem 并且我试图将模态放入我的 application.html.erb
以便模态始终出现在顶部但我收到的错误消息是
undefined local variable or method `current_user' for ApplicationController:Class
要在Application.html.erb
中调用Post.rb
中的记录,我应该在哪里修复?感谢您的帮助!
Application.html.erb
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li><%=link_to 'Sign out', destroy_user_session_path, method: 'delete' %></li>
<%=render "post_list", post_list: @post_list%> //I tried to call this
<% else %>
<li><%=link_to 'Sign in', new_user_session_path %></li>
<li><%=link_to 'Sign up', new_user_registration_path %></li>
<% end %>
</ul>
应用程序控制器
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
@post_list = Post.where(user_id: current_user).list?
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
您可以在 ApplicationController
current_user
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def current_user_posts?
@post_list = Post.where(user_id: current_user).list? // current_user should work here.
end
end