I find a solution to my problem following many SO answers. But , i'm not so comfort with Rails magic , and i would understand what happened in my web app. I explain the context:
I have two model:
class Registry < ActiveRecord::Base
belongs_to :profession
accepts_nested_attributes_for :profession
end
class Profession < ActiveRecord::Base
has_many :registries
end
and a form (for registry
) with nested attributes of the profession
model
<%= form_for(@registry) do |f| %>
...
<%= f.fields_for :profession do |ff| %>
...
The problem was , that if i used:
<%= f.fields_for :profession do |ff| %>
I didn't show the attributes of profession table. Instead ,if i used
:professions
so:
<%= f.fields_for :professions do |ff| %>
I see the form. I don't understand why this happened but then , searching on google and on SO , I found that:
Nested attributes allow you to save attributes on associated records through the parent.
So , maybe this behaviour is caused cause Iām using nested attributes in the child table. But i would have explained better why with :professions
i saw the attributes of the form and with :profession
, no.
Now , i fixed the problem using this in the registries_controller
:
def new
@registry = Registry.new
@registry.build_profession
end
I added the @registry.build_profession
and now in the form i can use the singular ":profession"
I don't know what happened... Can someone explain me these two things? really, i'm not confort with Rails magic..
Also , i have a problem cause if i don't want to insert data in the part of profession form , the profession row is always created in the database. This row is linked by foreign_key to registry row , but is blank (if i don't write nothing in the form , of course). This is really unacceptable..why i have to create a row , before to check if the user put the values..this is not good. I know , nested attribute should be in the parent , but i have this necessity to use them in the child. Really, rails programmers didin't think this possibility? I programmed in rails for 6 months , it's not much, i know..but is it possible that everytime i have to fix or discover new things (completely new)??
I'm getting crazy.
EDIT: I'm serching explaination of the problem. Not to solve it.