dimanche 8 mai 2016

Ruby on Rails: ActiveRecord::AssociationTypeMismatch

I am getting an ActiveRecord::AssociationTypeMismatch error when trying to submit a record.

Subject(#88982676) expected, got String(#20223000)

View:

<%= f.collection_select :subject, Subject.order(:subject), :subject, :subject, {prompt: "Select a subject"}, {class: "form-control"} %>

Controller:

 def create
            @homework = current_user.homeworks.build(homework_params)
            if @homework.save
                redirect_to homeworks_path
            else
                render 'new'
            end
        end
...

def homework_params
            params.require(:homework).permit(:subject, :description, :date, :completed_at)
        end

Model: Homework.rb

class Homework < ActiveRecord::Base

 validates :subject, presence:true

    belongs_to :subject

def completed?
    !completed_at.blank?
end


end

Subject.rb

class Subject < ActiveRecord::Base

     has_many :homeworks

     def to_s
     subject
  end
end

This use to work but suddenly doesn't. I did change the name of the table to "subject" but nothing else and changed the views and controller accordingly. It appears to be looking for id now? Subject is a string. Any advice? Thanks.

Aucun commentaire:

Enregistrer un commentaire