Tried to create a form for assosiated model in rails but form_for giving error " NoMethodError in Entities#new " " undefined method `entities' for nil:NilClass"
Code for model
<!--Schema model -->
class Schema < ApplicationRecord
has_many :entities, dependent: :destroy
validates :name, presence: true, uniqueness: true
validates :schemaId, presence: true, uniqueness: true
end
<!--Entity Model -->
class Entity < ApplicationRecord
belongs_to :schema
end
<!-- Entity Controller -->
class EntitiesController < ApplicationController
def index
@schema = Schema.find_by(:id => params[:id])
@entities = @schema.entities
end
def new
@schema = Schema.find_by(:id => params[:id])
@entity = Entity.new
end
def create
@schema = Schema.find_by(:id => params[:id])
@entity = @schema.entities.new(entity_params)
@entity.save!
end
private
def entity_params
params.require(:entity).permit(:clientId, :name, :description, :mnemonic)
end
end
<!-- Entity_new_form-->
<%= form_for [@schema, @schema.entities.build] do |f| %>
<p>
<%= f.label :clientId %><br/>
<%= f.text_field :clientId %>
</p>
<p>
<%= f.label :name %><br/>
<%= f.text_area :name %>
</p>
<p>
<%= f.label :description %><br/>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :mnemonic %><br/>
<%= f.text_area :mnemonic %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
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.
Aucun commentaire:
Enregistrer un commentaire