ruby on rails - Audited: undefined method `associated' for nil:NilClass -
so have entry model column called created_by default first user created entry. installed audited , in entry model have:
belongs_to :user audited :associated_with => :user   similarly in user model have following statements:
has_many :entries      has_associated_audits   in entries form, trying create hidden_field in trying set created_by equal the associated user of first audit on entry. i'm getting error, when try load form view:
undefined method `associated' nil:nilclass   here code in view:
 .field    = f.label :created_by  %br    = f.hidden_field :created_by, value: @entry.audits.first.associated   and here code in entries_controller set @entry in view: 
  def new     @entry = entry.new     authorize! :create, @entry      @project = project.find(params[:project_id])     @entry.project_id = @project.id     authorize! :update, :read, @project   end   i realize that, intuitively, doesn't make sense call @entry.audits in event of creating  new audit, since has not yet been saved. should set value of @entry.created_by after call @entry.save in create method of controller? not sure practical approach take here. 
thanks,
micha'el.
what i'd remove created_by hidden field form (that intercepted), , instead set within controller before entry saved using current_user (or alternative) access user creating entry.
def create_entry   @entry.created_by = current_user   @entry.save end   i wouldn't consider important assign created_by user after audit has been saved via @entry.audits.first.associated because audits should associated same current_user. if isn't case, have bigger problems on hands. 
Comments
Post a Comment