我写了一个mixin(基于我在博客上读到的东西),似乎导致了一个问题
这是一个项目的链接:http://www.filehosting.org/file/details/263759/onlinescheduler.zip(或给我发电子邮件:aaron.a.ashworth@gmail.com,我会通过电子邮件发送),我已经剥离了尽可能多的,因为它仍然会导致问题。要查看的关键文件有:
/lib/user_role.rb (near line 11)
/app/views/customers/index.html.erb (near line 16)
/app/controllers/customers_controller.rb (near line 47)
我将在这里布局重要的内容:
/lib/user_role.rb:
module UserRole
def self.included(base)
base.has_one :user, :as => :user_role, :autosave => true
base.validate :user_must_be_valid
base.alias_method_chain :user, :autobuild
base.extend ClassMethods
base.define_user_accessors
end
def user_with_autobuild
user_without_autobuild || build_user
end
def method_missing(meth, *args, &blk)
user.send(meth, *args, &blk)
rescue NoMethodError
super
end
module ClassMethods
def define_user_accessors
all_attributes = User.content_columns.map(&:name) + ["password", "password_confirmation"]
ignored_attributes = ["created_at", "updated_at", "user_role_type"]
attributes_to_delegate = all_attributes - ignored_attributes
attributes_to_delegate.each do |attrib|
class_eval <<-RUBY
def #{attrib}
user.#{attrib}
end
def #{attrib}=(value)
self.user.#{attrib} = value
end
def #{attrib}?
self.user.#{attrib}?
end
RUBY
end
end
end
protected
def user_must_be_valid
Logger.new(STDOUT).info('calling user_must_be_valid')
unless user.valid?
user.errors.each do |attr, message|
errors.add(attr, message)
end
end
end
end
app/views/customers/index.html.erb:
...
<% @customers.each do |customer| %>
<tr>
<td><%= customer.account_id %></td>
...
访问customer
会导致问题。我可以对@customers
做任何事但是一旦我尝试访问customer....
甚至@customers[0]....
,我就会遇到问题。
生成
步骤1)解压缩文件后,进入终端的根目录,执行以下命令:
bundle install
bundle exec rake db:drop
bundle exec rake db:migrate
rails s
2)打开浏览器到localhost:3000/customers
,点击New Customer
3)按如下方式填写表格:
Account: 3
First Name: First
Last Name: Last
Email: first.last@domain.com
Password: 1234
Password confirmation: 1234
4)点击Create Customer
按钮。
你应该重定向到localhost:3000/customers/1
web服务器崩溃,因为你得到以下消息:
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17b356) [0x7fef4a97e356]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1713ee) [0x7fef4a9743ee]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x177243) [0x7fef4a97a243]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(rb_vm_invoke_proc+0x9f) [0x7fef4a97b08f]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17b644) [0x7fef4a97e644]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1713ee) [0x7fef4a9743ee]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x177243) [0x7fef4a97a243]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1784f4) [0x7fef4a97b4f4]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x178cb5) [0x7fef4a97bcb5]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x17b50d) [0x7fef4a97e50d]
~/.rvm/rubies/ruby-1.9.2-p290/lib/libruby.so.1.9(+0x1713ee) [0x7fef4a9743ee]
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
重新运行web服务器并转到localhost:3000/customers
有时会给你一个不同的错误。一个分段错误,它抱怨/lib/user_role.rb:11
Ruby 1.9.2-p290
Rails 3.0.9
RVM 1.8.1
Ubuntu 11.04
编辑
注意事项:如果你尝试在主机上运行相同的代码,它似乎没问题。例子:
(After entering rails c)
@customers = Customer.all
@customers.each do |customer|
p customer.account_id
end
# this doesn't cause an error or crash.
@customer[0].first_name
=> "First"
如果删除:
def method_missing(meth, *args, &blk)
customer.send(meth, *args, &blk)
rescue NoMethodError
super
end
和
def method_missing(meth, *args, &blk)
user.send(meth, *args, &blk)
rescue NoMethodError
super
end
从lib目录中的x_role文件中取出,它应该可以正常工作。另外,请查看控制器的继承资源和窗体的简单表单。