# Nested model form problem, not saving, any help?

**URL:** <https://www.ruby-forum.com/t/nested-model-form-problem-not-saving-any-help/184342>\
**Category:** Rails\
**Created:** [March 1, 2010, 9:18am UTC](https://www.ruby-forum.com/t/nested-model-form-problem-not-saving-any-help/184342 "2010-03-01T09:18:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramon\_De\_Paula\_Marq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@Ramon\_De\_Paula\_Marq](https://www.ruby-forum.com/u/Ramon_De_Paula_Marq)\
**Post date:** [March 1, 2010, 9:18am UTC](https://www.ruby-forum.com/t/nested-model-form-problem-not-saving-any-help/184342/1 "2010-03-01T09:18:17Z")

</div>

I have two models, Curso and Ementa

class Curso \< ActiveRecord::Base

attr\_accessible :nome, :descricao, :periodo\_inscricao\_inicio,

> :periodo\_inscricao\_fim

> has\_one :ementa, :dependent =\> :destroy

has\_many :turmas

has\_many :publico\_alvos

> accepts\_nested\_attributes\_for :ementa

end

class Ementa \< ActiveRecord::Base

belongs\_to :curso

end

One form for hadle both models

\<% form\_for @curso do |f| %\>

\<%= f.error\_messages %\>

```
<%= f.label :nome %>

<br/>

<%= f.text_field :nome %>

```

```
<%= f.label :descricao %>

<br/>

<%= f.text_area :descricao %>

```

```
<%= f.label :periodo_inscricao_inicio %>

<br/>

<%= f.date_select :periodo_inscricao_inicio %>

```

```
<% fields_for :ementa do |ementa_form| %>

```

\<%= ementa\_form.label :descricao, “Ementa” %\>

```
<%= ementa_form.text_field :descricao %>

```

\<% end %\>

```
<%= f.label :periodo_inscricao_fim %>

<br/>

<%= f.date_select :periodo_inscricao_fim %>

```

```
<%= f.submit "Submit" %>

```

\<% end %\>

the controller

class CursosController \< ApplicationController

def index

```
@cursos = Curso.all

```

end

def show

```
@curso = Curso.find(params[:id])

```

> ```
> respond_to do |format|
> 
> ```

```
  format.html {render :layout=>false}

end

```

end

def new

```
@curso = Curso.new

@curso.build_ementa

```

> ```
> respond_to do |format|
> 
> ```

```
  format.html {render :layout=>false}

end

```

end

def create

```
@curso = Curso.new(params[:curso])

if @curso.save

  flash[:notice] = "Successfully created curso."

  redirect_to @curso

else

  render :action => 'new'

end

```

end

def edit

```
@curso = Curso.find(params[:id])

```

> ```
> respond_to do |format|
> 
> ```

```
  format.html {render :layout=>false}

end

```

end

def update

```
@curso = Curso.find(params[:id])

if @curso.update_attributes(params[:curso])

  flash[:notice] = "Successfully updated curso."

  redirect_to @curso

else

  render :action => 'edit'

end

```

end

def destroy

```
@curso = Curso.find(params[:id])

@curso.destroy

flash[:notice] = "Successfully destroyed curso."

redirect_to cursos_url

```

end

end

The result is just one insert on curso table, why it is not inserting on  
ementa table to the “descricao” field?

Console result:

Processing CursosController#create (for 127.0.0.1 at 2010-03-01  
00:46:32)

> [POST]

Parameters:

> {“authenticity\_token”=\>“aZzxtPN1y4nOWB3GwAAWSqGfU1iEPIiL2+rbEjjIr3k=”,  
> “curso”=\>{“nome”=\>“Test”, “descricao”=\>“test”,  
> “periodo\_inscricao\_inicio(3i)”=\>“1”, “periodo\_inscricao\_inicio(2i)”=\>“3”,  
> “periodo\_inscricao\_inicio(1i)”=\>“2010”, “periodo\_inscricao\_fim(3i)”=\>“1”,  
> “periodo\_inscricao\_fim(2i)”=\>“3”, “periodo\_inscricao\_fim(1i)”=\>“2010”},  
> “ementa”=\>{“descricao”=\>“test”}, “commit”=\>“Submit”}

[4;36;1mSQL (1.0ms) [0m [0;1mBEGIN [0m

[4;35;1mSQL (2.0ms) [0m [0mINSERT INTO “cursos” (“nome”,  
“descricao”,

> “periodo\_inscricao\_inicio”, “periodo\_inscricao\_fim”, “created\_at”,  
> “updated\_at”) VALUES(E’Test’, E’test’, ‘2010-03-01’, ‘2010-03-01’,  
> ‘2010-03-01 03:46:32.756995’, ‘2010-03-01 03:46:32.756995’) RETURNING  
> “id” [0m

[4;36;1mSQL (2.0ms) [0m [0;1mCOMMIT [0m

–  
Ramon de Paula Marques  
Estudante de Engenharia de ComputaÃ§Ã£o - UFG
