title: form_for NoMethodError in Entities#new
body: <p>Tried to create a form for assosiated model in rails but form_for giving error
  &quot; NoMethodError in Entities#new &quot;
  &quot; undefined method `entities' for nil:NilClass&quot;</p>
  <p>Code for model</p>
  <pre><code>&lt;!--Schema model --&gt;
  
  class Schema &lt; ApplicationRecord
    has_many :entities, dependent: :destroy
  
    validates :name, presence: true, uniqueness: true
    validates :schemaId, presence: true, uniqueness: true
  end
  
  &lt;!--Entity Model --&gt;
  
  class Entity &lt; ApplicationRecord
    belongs_to :schema
  end
  
  &lt;!-- Entity Controller --&gt;
  class EntitiesController &lt; ApplicationController
  
    def index
      @schema = Schema.find_by(:id =&gt; params[:id])
      @entities = @schema.entities
    end
  
  
    def new
        @schema = Schema.find_by(:id =&gt; params[:id])
        @entity = Entity.new
    end
  
    def create
      @schema = Schema.find_by(:id =&gt; params[:id])
      @entity = @schema.entities.new(entity_params)
      @entity.save!
    end
  
    private
  
      def entity_params
     params.require(:entity).permit(:clientId, :name, :description, :mnemonic, :schema_id)
  </code></pre>
  <p>end
  end</p>
  <pre><code>&lt;!-- Entity_new_form--&gt;
  &lt;%= form_for [@schema, @schema.entities.build] do |f| %&gt;
    &lt;p&gt;
      &lt;%= f.label :clientId %&gt;&lt;br/&gt;
      &lt;%= f.text_field :clientId %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :name %&gt;&lt;br/&gt;
      &lt;%= f.text_area :name %&gt;
    &lt;/p&gt;
  
    &lt;p&gt;
      &lt;%= f.label :description %&gt;&lt;br/&gt;
      &lt;%= f.text_area :description %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.label :mnemonic %&gt;&lt;br/&gt;
      &lt;%= f.text_area :mnemonic %&gt;
    &lt;/p&gt;
    &lt;p&gt;
      &lt;%= f.submit 'Submit' %&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>This is giving error here entity is associated with Schema... I have to take input from user for new entity but this form is giving me error.</p>
  <p>rails console is working fine:</p>
  <pre><code>Loading development environment (Rails 7.0.3)
  irb(main):001:0&gt; en = Entity.new(clientId:&quot;1&quot;,name:&quot;Allergy&quot;,description:&quot;Infected By&quot;,mnemonic:&quot;Allergy&quot;,schema_id:14)
     (10.1ms)  SELECT sqlite_version(*)
  =&gt;
  #&lt;Entity:0x000055f1e5e55290
  ...
  irb(main):002:0&gt; en.save
    TRANSACTION (0.1ms)  begin transaction
    Schema Load (0.9ms)  SELECT &quot;schemas&quot;.* FROM &quot;schemas&quot; WHERE &quot;schemas&quot;.&quot;id&quot; = ? LIMIT ?  [[&quot;id&quot;, 14], [&quot;LIMIT&quot;, 1]]
    Entity Create (66.3ms)  INSERT INTO &quot;entities&quot; (&quot;clientId&quot;, &quot;name&quot;, &quot;description&quot;, &quot;mnemonic&quot;, &quot;schema_id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES (?, ?, ?, ?, ?, ?, ?)  [[&quot;clientId&quot;, &quot;1&quot;], [&quot;name&quot;, &quot;Allergy&quot;], [&quot;description&quot;, &quot;Infected By&quot;], [&quot;mnemonic&quot;, &quot;Allergy&quot;], [&quot;schema_id&quot;, 14], [&quot;created_at&quot;, &quot;2022-06-12 06:15:41.888670&quot;], [&quot;updated_at&quot;, &quot;2022-06-12 06:15:41.888670&quot;]]
    TRANSACTION (21.9ms)  commit transaction
  =&gt; true
  </code></pre>