Rails 关联 has_many 和 belongs_to 无效
Rails associations has_many and belongs_to not working
ArgumentError in ProposalsController#index
No association found for name `clients'. Has it been defined yet?
我在我的应用程序上遇到了这个错误。无法找到修复它的解决方案。有什么想法吗?
这是模态框:
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
- 我已将“client_id”放入文档的数据库中。
- 我已经在两个控制器中设置了所有安全参数。
您快完成了,只是漏掉了一个小细节。始终遵循解释器/编译器所说的内容,您可以找到问题所在。
在你的情况下 :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
ArgumentError in ProposalsController#index
No association found for name `clients'. Has it been defined yet?
我在我的应用程序上遇到了这个错误。无法找到修复它的解决方案。有什么想法吗?
这是模态框:
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
- 我已将“client_id”放入文档的数据库中。
- 我已经在两个控制器中设置了所有安全参数。
您快完成了,只是漏掉了一个小细节。始终遵循解释器/编译器所说的内容,您可以找到问题所在。
在你的情况下 :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