Vanity gem 在我的用户模型上抛出错误
我完美地遵循了 gem 的说明 - https://github.com/jaustinhughey/vanities - 以及一切其他工作正常。
但是,当我尝试加载主页时,出现此错误:
NameError in HomeController#index
undefined local variable or method `has_vanity' for #<Class:0x000001015691e8>
app/models/user.rb:2:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/home_controller.rb:3:in `index'
这是我的 User.rb
class User < ActiveRecord::Base
has_vanity
has_many :feedbacks_as_poster, :foreign_key => :poster_id, :class_name => 'Feedback'
has_many :feedbacks_as_receiver, :foreign_key => :receiver_id, :class_name => 'Feedback'
end
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255)
# f_name :string(255)
# l_name :string(255)
# username :string(255)
# role_id :integer
# picture :string(255)
# about_me :string(255)
# website :string(255)
# created_at :datetime
# updated_at :datetime
#
这是我的 Home Controller
class HomeController < ApplicationController
def index
@users = User.all
@feedback = Feedback.new
end
end
想法?
编辑 1:请参阅下面的 Gemfile
:
source 'http://rubygems.org'
gem 'rails', '3.1.0'
gem 'execjs'
gem 'therubyracer'
gem 'vanities'
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem 'jquery-rails'
group :development do
gem 'sqlite3'
gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
end
group :test do
gem 'turn', :require => false
end
group :production do
gem 'pg', :require => 'pg'
end
编辑 2:如果我转到我创建的用户的 URL 配置文件,例如 localhost:3000/test
,我会看到以下错误,这让我相信 gem 的一部分正在工作,因为它实际上获取了正确的记录。但还有其他东西坏了。
Started GET "/test" for 127.0.0.1 at 2011-09-07 20:00:19 -0500
Processing by VanitiesController#show as HTML
Parameters: {"vname"=>"test"}
Vanity Load (0.2ms) SELECT "vanities".* FROM "vanities" WHERE "vanities"."name" = 'test' LIMIT 1
Completed 500 Internal Server Error in 100ms
NameError (undefined local variable or method `has_vanity' for #<Class:0x0000010313d478>):
app/models/user.rb:2:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/vanities_controller.rb:8:in `show'
I followed the instructions for the gem perfectly - https://github.com/jaustinhughey/vanities - and everything else worked fine.
But when I tried to load my main page, I am getting this error:
NameError in HomeController#index
undefined local variable or method `has_vanity' for #<Class:0x000001015691e8>
app/models/user.rb:2:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/home_controller.rb:3:in `index'
Here is my User.rb
class User < ActiveRecord::Base
has_vanity
has_many :feedbacks_as_poster, :foreign_key => :poster_id, :class_name => 'Feedback'
has_many :feedbacks_as_receiver, :foreign_key => :receiver_id, :class_name => 'Feedback'
end
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255)
# f_name :string(255)
# l_name :string(255)
# username :string(255)
# role_id :integer
# picture :string(255)
# about_me :string(255)
# website :string(255)
# created_at :datetime
# updated_at :datetime
#
And here is my Home Controller
class HomeController < ApplicationController
def index
@users = User.all
@feedback = Feedback.new
end
end
Thoughts?
Edit 1: See my Gemfile
below:
source 'http://rubygems.org'
gem 'rails', '3.1.0'
gem 'execjs'
gem 'therubyracer'
gem 'vanities'
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem 'jquery-rails'
group :development do
gem 'sqlite3'
gem 'annotate', :git => 'git://github.com/ctran/annotate_models.git'
end
group :test do
gem 'turn', :require => false
end
group :production do
gem 'pg', :require => 'pg'
end
Edit 2: If I go to the URL profile of a user I have created, e.g. localhost:3000/test
I see the following error, which leads me to believe that part of the gem is working because it actually fetches the correct record. But something else is broken.
Started GET "/test" for 127.0.0.1 at 2011-09-07 20:00:19 -0500
Processing by VanitiesController#show as HTML
Parameters: {"vname"=>"test"}
Vanity Load (0.2ms) SELECT "vanities".* FROM "vanities" WHERE "vanities"."name" = 'test' LIMIT 1
Completed 500 Internal Server Error in 100ms
NameError (undefined local variable or method `has_vanity' for #<Class:0x0000010313d478>):
app/models/user.rb:2:in `<class:User>'
app/models/user.rb:1:in `<top (required)>'
app/controllers/vanities_controller.rb:8:in `show'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我试图复制这个问题,但我似乎无法复制它,这意味着它可能是您的应用程序特有的东西导致了问题。
您可以参考 https://github.com/jaustinhughey/vtest 作为工作示例。
我发现您的问题有趣的是:
未定义的局部变量或方法`has_vanity' for #<Class:0x000001015691e8>
那不应该是“用户”,而不是“类”吗? Vanities 的工作原理是将对象和 Vanity 的多态关联作为可用方法推送到 ActiveRecord 中。然后,您只需调用“has_vanity”即可自动获得可用的多态关联。这是一种简单的方法——实际上只是一条捷径。
我想知道在您的情况下是否还有其他“时髦”的事情正在对 ActiveRecord 进行。我不确定具体是什么会导致这种情况,但由于某种原因,在您的情况下,对于 User 对象来说,该方法根本不存在。
作为解决方法,您可以尝试添加以下代码来代替对“has_vanity”的调用:
I've tried to duplicate this problem on my end, and I can't seem to get it to duplicate, meaning it's possibly something unique to your application that may be causing the issue.
You can reference https://github.com/jaustinhughey/vtest for a working example.
What I find interesting about your problem is this:
undefined local variable or method `has_vanity' for #<Class:0x000001015691e8>
Shouldn't that be "User", not "Class"? Vanities works by pushing up the polymorphic association for an object and a vanity into ActiveRecord as an available method. Then you simply call "has_vanity" to automatically have that polymorphic association available for you. It's a simplistic way to get it in there - only a shortcut, really.
I'm wondering if there's something else "funky" that's being done to/with ActiveRecord in your case. I'm not sure what specifically would cause that, but for some reason that method just plain doesn't exist for a User object in your case.
As a work-around, you may try adding the following code in place of the call to "has_vanity":