railstutorial.org - 让新用户关注管理员用户
railstutorial.org - make new users follow admin users
我已经按照 Michael Hartl 的 Ruby 在 Rails 教程制作类似 Twitter 的应用程序,我想更改它以便所有新用户自动 'follow' Admin/s.
我尝试按照对类似问题 here and here 的回答进行操作,但它们会在创建帐户时出错。这是我的用户控制器的相关部分。
编辑:find_all_by_admin
在 Rails 4.2.0 中已弃用,如 Vinay 在所选答案中所述。
controllers/users_controller.rb
class UsersController < ApplicationController
...
def follow_admins
admins = User.find_all_by_admin(true) # EDIT - Deprecated: May have worked prior to rails 4.2
admins.each do |admin|
self.follow!(admin)
end
end
错误信息是
UserController#create 中没有方法错误
undefined method `find_all_by_admin' for #
您可能从未在 User
模型中定义 find_all_by_admin
方法。
打开User
模型文件,检查方法是否存在。如果不是,则将其定义为 class 方法。
在我看来,您的 User model
正如我们可能看到的 Michael Hartl's sample_app_3rd_edition
因此,为了使 follow_admins
方法起作用,您需要 在用户中添加管理列 table 输入布尔值和默认值:false.
def follow_admins
admins = User.find_all_by_admin(true) # would be worked in rails 4.0 not rails 4.2.2
admins = User.where(admin: true) # Should be work in rails 4.2.2
# Most of the Dynamic finder has been removed form rails 4.2.2
admins.each do |admin|
self.follow!(admin)
end
end
注意 正如我在回答 default to false
中提到的,这不是强制性的,但因为您在 Rails 上关注 Michael Hartl 的 Ruby教程照着做就好了。
希望这个回答对您有所帮助!!!
我已经按照 Michael Hartl 的 Ruby 在 Rails 教程制作类似 Twitter 的应用程序,我想更改它以便所有新用户自动 'follow' Admin/s.
我尝试按照对类似问题 here and here 的回答进行操作,但它们会在创建帐户时出错。这是我的用户控制器的相关部分。
编辑:find_all_by_admin
在 Rails 4.2.0 中已弃用,如 Vinay 在所选答案中所述。
controllers/users_controller.rb
class UsersController < ApplicationController
...
def follow_admins
admins = User.find_all_by_admin(true) # EDIT - Deprecated: May have worked prior to rails 4.2
admins.each do |admin|
self.follow!(admin)
end
end
错误信息是
UserController#create 中没有方法错误
undefined method `find_all_by_admin' for #
您可能从未在 User
模型中定义 find_all_by_admin
方法。
打开User
模型文件,检查方法是否存在。如果不是,则将其定义为 class 方法。
在我看来,您的 User model
正如我们可能看到的 Michael Hartl's sample_app_3rd_edition
因此,为了使 follow_admins
方法起作用,您需要 在用户中添加管理列 table 输入布尔值和默认值:false.
def follow_admins
admins = User.find_all_by_admin(true) # would be worked in rails 4.0 not rails 4.2.2
admins = User.where(admin: true) # Should be work in rails 4.2.2
# Most of the Dynamic finder has been removed form rails 4.2.2
admins.each do |admin|
self.follow!(admin)
end
end
注意 正如我在回答 default to false
中提到的,这不是强制性的,但因为您在 Rails 上关注 Michael Hartl 的 Ruby教程照着做就好了。
希望这个回答对您有所帮助!!!