代码之家  ›  专栏  ›  技术社区  ›  DanielsV

Rails关联有很多,属于不工作

  •  0
  • DanielsV  · 技术社区  · 7 年前

    ProposalsController#索引中的ArgumentError

    未找到名称“clients”的关联。它被定义了吗?

    这是我在应用程序上遇到的错误。无法找到解决方案来修复它。有什么想法吗?

    这是情态动词:

    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
    
    1. 我在db中输入了“client\u id”以获取文档。
    2. 我已经在两个控制器中放置了所有安全参数。
    1 回复  |  直到 4 年前
        1
  •  1
  •   Ziyan Junaideen    7 年前

    你就快到了,只是错过了一个小细节。始终遵循解释器/编译器所说的内容,您可以发现哪里出了问题。

    对你来说 :clients 在中应为单数 accepts_nested_attributes_for 因为它是属于

    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