我正在关注 onebitcode 教程,但我无法实现这个 gem,我更新了数据库,我上传了种子,我重新启动了服务器,但没有任何效果,如果我在 GemFile 中评论 gem,它会再次工作,但没有 cancancan ...错误在浏览器中弹出,但我不知道如何解决它。
#Error
Run Code Online (Sandbox Code Playgroud)
RailsAdmin::MainController#dashboard 中的 CanCan::AccessDenied
您无权访问此页面。
提取的源代码(围绕第 217 行):
if cannot?(action, subject, *args)
message ||= unauthorized_message(action, subject)
raise AccessDenied.new(message, action, subject)
end
subject
end
#Ability.rb
class Ability
include CanCan::Ability
def initialize(user)
end
end
#rails_admin.rb
RailsAdmin.config do |config|
## == Devise ==
config.authenticate_with do
warden.authenticate! scope: :user
end
config.current_user_method(&:current_user)
##== Cancancan ==
config.authorize_with :cancan
config.actions do
dashboard # mandatory
index # mandatory
new
export
bulk_delete
show
edit
delete
show_in_app
end
end
#gemfile
source …Run Code Online (Sandbox Code Playgroud) 我有很多用户,如何只列出一个帖子,还显示其他用户的所有评论?
发布模型
belongs_to :user
has_many :comments
Run Code Online (Sandbox Code Playgroud)
评论模型
belongs_to :post
belongs_to :user
Run Code Online (Sandbox Code Playgroud)
用户模型
has_many :posts
has_many :comments
Run Code Online (Sandbox Code Playgroud)
的routes.rb
resources :posts do
resources :comments
end
Run Code Online (Sandbox Code Playgroud)
在控制器中
def index
@post = User.posts.all
@comment = User.comments.all
end
Run Code Online (Sandbox Code Playgroud)
在视图index.html.erb中
<%@post.each do |post|%>
<%=post.post_name %>
<%= post.post_description %>
<%end%>
<%@comment.each do |comment|%>
<%=comment.content %>
<%end%>
Run Code Online (Sandbox Code Playgroud)
如何仅显示当前用户的帖子和评论?