gpt4 book ai didi

Ruby/Rails : accessing a variable inside a . 在我的实例变量数组上导致 Ruby 解释器崩溃

转载 作者:数据小太阳 更新时间:2023-10-29 08:01:38 25 4
gpt4 key购买 nike

我写了一个 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) 点击创建客户按钮。

预期行为

您应该被重定向到 localhost:3000/customers/1

当前行为

当您收到以下消息时,网络服务器崩溃了:

~/.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

重新运行网络服务器并转到 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 文件,它应该可以正常工作。在旁注中查看 Controller 的继承资源和表单的简单表单。

关于Ruby/Rails : accessing a variable inside a . 在我的实例变量数组上导致 Ruby 解释器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7314987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com