Papertrail undefined方法`klass'为零:NilClass

时间:2015-06-03 11:38:00

标签: ruby ruby-on-rails-4 rubygems paper-trail-gem

我的控制器中有这个恢复方法

def revert
  version = PaperTrail::Version.find(params[:id])
  if version.reify
    version.reify(:has_many=>true).save!
  else
    version.item.destroy
  end
  share_type = ShareType.find(params[:share_type_id])
  redirect_to :back
end

我收到了未定义方法`klass'的错误为nil:NilClass

version.reify(:has_many=>true).save!

但是当我尝试

version.reify.save!

一切都变得很好我做错了我正在使用其他资源的恢复方法

def revert
  version = PaperTrail::Version.find(params[:id])
  if version.reify
    version.reify(has_many: true).save!
  else
    version.item.destroy
  end
  company = Company.find_by_slug(params[:company_id])
  redirect_to :back
end

这可以解决任何错误。 这是我的两种资源模型

class ShareType < ActiveRecord::Base

  has_paper_trail
  belongs_to :company 
  has_many :issue_shares, dependent: :destroy
  has_many :users, through: :shares

  validates :company, :code, :name, :reason, :effective_date, presence: true

  before_save :convert_new_line_to_html

  def convert_new_line_to_html
    self.description.to_s.gsub!(/\r\n?/, "<br>")
  end

end

和公司

class Company < ActiveRecord::Base
  extend FriendlyId
  friendly_id :slug_candidates, use: :slugged
  has_many :user_companies, dependent: :destroy
  has_many :users, through: :user_companies
  has_many :advisor_invitations
  has_many :company_advisors
  has_many :advisors, through: :company_advisors

  belongs_to :created_by, class_name: "User", foreign_key: "user_id"

  has_many :share_types, dependent: :destroy
  validates :user_id, :name,:company_no,:company_type,:registered_office, :principle_place, presence: true
  validates_uniqueness_of :name, :company_no

  validates_presence_of :register_address_occupier, :if => lambda { |o| !o.occupied_by_company }
  has_paper_trail

  def slug_candidates
    [
      :name,
      [:name, :company_type],
      [:name, :company_type, :company_no],
   ]
  end
  def should_generate_new_friendly_id?
     slug.blank? || name_changed?
  end
  def is_admin?(user)
    !user_companies.where("user_id=? AND role=?",user.id, 0).empty?
  end
  def share_holder?(user)
    shares = Share.includes(issue_share:[:share_type]).where("share_types.company_id=? AND shares.user_id=?", id, user.id).references(:share_types)
    !shares.empty?
  end

  def is_director?(user)
    !user_companies.where("user_id=? AND role=?",user.id, 1).empty?
  end

  def is_advisor?(user)
    !advisors.where("user_id=?",user.id).empty?
  end
end

1 个答案:

答案 0 :(得分:0)

我解决了存在关联的问题

has_many :users, through: :shares
在我没有

的情况下,在share_types中

has_many :shares

所以我需要删除这一行

has_many :users, through: :shares

我不需要这解决了我的问题。