3

我写了一个似乎导致问题的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

当前行为

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

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

1 回答 1

3

如果您删除:

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 文件中,它应该可以正常工作。在旁注中查看控制器的继承资源和表单的简单表单。

于 2011-09-08T13:59:14.893 回答