Ruby on Rails中Followability gem的mutual_following_with?方法报错求助
问题修复方案
这个报错是因为gem里的Followable模块在引用Relationship模型时,命名空间路径写错了——它在Followability::Followable子命名空间下去找Relationship,但正确的路径应该是Followability::Relationship。
可以按以下步骤解决:
先尝试更新gem
运行命令更新到最新版本,说不定官方已经修复了这个bug:bundle update followability如果更新无效,用Monkey Patch修复
在项目的config/initializers目录下新建一个文件(比如followability_fix.rb),粘贴以下代码:module Followability module Followable def unfollow(target) # 明确指定正确的命名空间 relationship = Followability::Relationship.find_by(follower: self, following: target) relationship&.destroy end def mutual_following_with?(target) following?(target) && target.following?(self) end end end重启服务器后再测试取消关注功能,这个补丁会覆盖gem里有问题的方法,强制使用正确的
Relationship路径。额外检查
确认你的用户模型(比如User.rb)里已经正确包含了gem的模块:class User < ApplicationRecord include Followability::Follower include Followability::Followable end
这个问题大概率是gem的小bug,不是你代码的问题,用上面的方法就能解决。
内容的提问来源于stack exchange,提问作者daniel
相关产品推荐
相关产品推荐

