Ruby on rails 3 Rails 3.2+;Rolify:安装Rolify后应用程序中断(Bug?)

Ruby on rails 3 Rails 3.2+;Rolify:安装Rolify后应用程序中断(Bug?),ruby-on-rails-3,Ruby On Rails 3,我跑得很好。我通过以下步骤安装: 向gem文件中添加gem“rolify” 运行bundle安装 运行rails g角色化:角色 检查新迁移、新文件和修改的文件(由上面的命令生成/修改) 运行rake db:migrate 此时,我尝试创建/编辑一个用户,但出现以下错误: NoMethodError in UsersController#create undefined method `user_id' for #<User:0x007f8f21f168e8> 用户控制器中的No

我跑得很好。我通过以下步骤安装:

  • 向gem文件中添加
    gem“rolify”
  • 运行
    bundle安装
  • 运行rails g角色化:角色
  • 检查新迁移、新文件和修改的文件(由上面的命令生成/修改)
  • 运行rake db:migrate
  • 此时,我尝试创建/编辑一个用户,但出现以下错误:

    NoMethodError in UsersController#create
    
    undefined method `user_id' for #<User:0x007f8f21f168e8>
    
    用户控制器中的NoMethodError#create 未定义的方法“用户id”# 请注意,在我安装Rolify之前,一切正常,因此问题来自Rolify

    以下是迁移、新文件和修改后的文件:

    新移民:

    class RolifyCreateRoles < ActiveRecord::Migration
      def change
        create_table(:roles) do |t|
          t.string :name
          t.references :resource, :polymorphic => true
    
          t.timestamps
        end
    
        create_table(:users_roles, :id => false) do |t|
          t.references :user
          t.references :role
        end
    
        add_index(:roles, :name)
        add_index(:roles, [ :name, :resource_type, :resource_id ])
        add_index(:users_roles, [ :user_id, :role_id ])
      end
    end
    
    class-RolifyCreateRolestrue
    t、 时间戳
    结束
    创建表(:users\u roles,:id=>false)do | t|
    t、 参考资料:用户
    t、 参考:角色
    结束
    添加索引(:角色,:名称)
    添加索引(:角色,[:名称,:资源类型,:资源id])
    添加索引(:users\u roles,[:user\u id,:role\u id])
    结束
    结束
    
    新模式:

    class Role < ActiveRecord::Base
      has_and_belongs_to_many :users, :join_table => :users_roles
      belongs_to :resource, :polymorphic => true
    end
    
    类角色:users_角色
    属于:资源,:多态=>true
    结束
    
    修改后的模型:

    class User < ActiveRecord::Base
      rolify
      has_secure_password
    
      has_many :issues
      acts_as_tenant(:client)
    
      attr_accessible :email, :password, :password_confirmation, :username
    
      validates :username, presence: true,
                           length: { within: 4..50 },
                           format: { with: /(?:[\w\d]){4,255}/ }
      validates_uniqueness_to_tenant :username, case_sensitive: false
    
      validates :email, presence: true,
                        uniqueness: { case_sensitive: false },
                        length: { within: 8..255 },
                        format: { with: /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i }
    
      validates :password, presence: true, on: :create,
                           confirmation: true,
                           length: { within: 4..255 }
    
      validates :password_confirmation, presence: true, on: :create
    
      # NOTE: Used by SimpleForm to display the dropdown proerply
      def to_label
        "#{username}"
      end
    end
    
    class用户
    您可以在中找到项目中的其余文件


    有人知道错误来自何处吗?

    发生此错误的原因是,代理租户(错误地)正在为
    用户
    模型上的
    用户id
    字段创建验证。如果在rails c中运行此代码,则可以看到此验证器:

     User._validators
    

    我建议切换到gem,它似乎比acts\u as\u tenant更易于维护。

    发生此错误是因为acts\u as\u tenant(错误地)正在为
    用户
    模型上的
    用户id
    字段创建验证。如果在rails c中运行此代码,则可以看到此验证器:

     User._validators
    
    我建议转用gem,它似乎比acts_as_租户更容易维护