Using rails 4, when I want to render a form (with simple_form) from an object Document::Document I have this error: undefined method document_type_id for #<Document::Document:0x007fada4a50240>
Here a part of my model:
class Document::Document < ActiveRecord::Base
...
belongs_to :document_type, -> {include(:translations)}, :class_name => 'Document::Type'
...
end
The method new of my controller:
def new
@document = Document::Document.new
end
And a part of the form with simple_form:
=f.association :document_type, prompt: t('document.documents.form.choose_document_type'), collection: Document::Type.includes(:translations)
The error:
undefined method `document_type_id' for #<Document::Document:0x007fada4a50240>
Extracted source (around line #14):
11 .row
12 =f.input :language, collection: languages_list, prompt: t("document.documents.form.choose_language"), label_html: tooltip(t('document.documents.forms.tooltips.language')), wrapper_html: {class: 'columns large-4'}, input_html: {class: 's2'}
13 =f.input :study_level, prompt: t('document.documents.form.choose_study_level'), label_html: tooltip(t('document.documents.forms.tooltips.study_level')), wrapper_html: {class: 'columns large-4'}, input_html: {class: 's2'}
14 =f.association :document_type, prompt: t('document.documents.form.choose_document_type'), collection: Document::Type.includes(:translations), label_html: tooltip(t('document.documents.forms.tooltips.type')), wrapper_html: {class: 'columns large-4'}, input_html: {class: 's2'}
15 -#=f.association :domains, collection: Domain.includes(:translations).order('name ASC'), label_html: tooltip(t('document.documents.forms.tooltips.domains')), input_html: {class: 's2'}
16 .form-actions
17 =f.button :submit, t('document.documents.form.submit')
Why this error ?
I upgraded from rails 3.2. Before everything works great.
In rails 3.2 I had added that:
attr_accessible :document_type_id, ...
Maybe the error is coming from there
Sorry for the inconvenient.
It was an issue with migration. Someone else changed the migration file and the field
document_type_id
was no more present in the table of the database.It took me a while until I find this stupid issue.
Thanks for the reply and apologise
Check the version of the simple_form Gem. Should be >= 3.0.0 Not sure how the simple_form gem works, but rails 4 doesn't use attr_accessible anymore (still, you can install the gem, but it's likely to be deprecated soon)
Rails 4 now uses Strong Parameters. Basically works like this: you define in a private method in the controller which parameters are whitelisted to be used, so if your document_type_id is not in the list it is ignored.
Rails 4 scaffold generator automatically adds this method and accepts all params by default if you want to see what it looks like.
Hope it helps!