Nested form with embeds_one

523 views
Skip to first unread message

iEmpire

unread,
Feb 22, 2011, 12:08:41 AM2/22/11
to Mongoid
Hi! I fallowed this topic
http://groups.google.com/group/mongoid/browse_thread/thread/f294f524d91d884f/efa83dc6f6b627c5?lnk=gst&q=form#efa83dc6f6b627c5
and realized such code


class Company
include Mongoid::Document
include Mongoid::Timestamps
field :title, :type => String
field :rating, :type => Float

embeds_one :company_profile
end

class CompanyProfile
include Mongoid::Document
field :phone, :type => String
field :www, :type => String
field :public_email, :type => String
field :working_hours, :type => String

embedded_in :company, :inverse_of => :company_profile
end

Companies controller method:

# GET /companies/new
# GET /companies/new.xml
def new
@company = Company.new
@company.build_company_profile

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @company }
end
end


And the _form template:

<%= form_for(@company) do |f| %>
<% if @company.errors.any? %>
.... error handling
<% end %>

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :rating %><br />
<%= f.text_field :rating %>
</div>
<% f.fields_for :company_profile do |builder| %>
<p>
<%= builder.label :public_email, "Public email" %>
<%= builder.text_field :public_email %><br />
<%= builder.label :www, "WWW" %>
<%= builder.text_field :www %><br />
</p>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


It doesn't works and nested form does not displaying! When it tried to
use <%=debug :company_profile %>, it printed only
"--- :company_profile"

Could you help me?

jmaicher

unread,
Feb 22, 2011, 10:45:44 AM2/22/11
to Mongoid
Hello iEmpire,

the following code should work. You have to use <%= ... %> with
fields_for.

<%= f.fields_for :company_profile do |builder| %>
<p>
<%= builder.label :public_email, "Public email" %>
<%= builder.text_field :public_email %><br />
<%= builder.label :www, "WWW" %>
<%= builder.text_field :www %><br />
</p>
<% end %>

Julian

On 22 Feb., 06:08, iEmpire <razor....@gmail.com> wrote:
> Hi! I fallowed this topichttp://groups.google.com/group/mongoid/browse_thread/thread/f294f524d...
Reply all
Reply to author
Forward
0 new messages