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 %>
<%= f.text_field :nome %><%= f.label :descricao %>
<%= f.text_area :descricao %><%= f.label :periodo_inscricao_inicio %>
<%= 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 %>
<%= 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: