ProposalsController中的ArgumentError #index
找不到名称`客户'的关联。它已被定义了吗?
我的应用上出现此错误。无法找到解决方案来解决问题。有任何想法吗?
这是模态:
class Client < ActiveRecord::Base
has_paper_trail
has_many :documents
accepts_nested_attributes_for :documents, allow_destroy: true, reject_if: :all_blank
end
和
class Document < ActiveRecord::Base
has_paper_trail
belongs_to :client
accepts_nested_attributes_for :clients, allow_destroy: true, reject_if: :all_blank
validates :name, presence: true
end
这是文件的控制器:
def index
if current_user.admin?
@documents = Document.paginate(page: params[:page], :per_page => 20)
else
@documents = Document.where("user_id = ?", current_user).paginate(page: params[:page], :per_page => 20)
end
end
答案 0 :(得分:1)
你快到了,只是错过了一个小细节。始终遵循解释器/编译器所说的内容,您可以找到出错的地方。
在您的情况下,:clients
在accepts_nested_attributes_for
中应该是单数的,因为它是belongs_to
class Document < ActiveRecord::Base
has_paper_trail
belongs_to :client
accepts_nested_attributes_for :client, allow_destroy: true, reject_if: :all_blank
validates :name, presence: true
end