views:

586

answers:

2

So here's the scenario:

User:

has_one :company
accepts_nested_attributes_for :company

Controller:

@user = User.new
@user.build_company

View:

<% semantic_form_for @user, :url => register_path do |form| %>

  <h2>User Information</h2>

  <%= form.inputs %>

  <h2>Company Information</h2>

  <% form.semantic_fields_for :company do |company| %>
    <%= company.inputs %>
  <% end %>

  <%= form.buttons %>

<% end %>

After scouring the web, this SEEMS like it should work. However, all I get are the user inputs. The "semantic_fields_for :company" block outputs nothing at all...

Am I missing something here, or is this perhaps a Rails 3 bug to do with Formtastic?

+1  A: 

You need to use "<%= %>" with Rails 3 blocks instead of "<% %>". Thus, the code should be:

<%= semantic_form_for @user, :url => register_path do |form| %>

  <h2>User Information</h2>

  <%= form.inputs %>

  <h2>Company Information</h2>

  <%= form.semantic_fields_for :company do |company| %>
    <%= company.inputs %>
  <% end %>

  <%= form.buttons %>

<% end %>
Kevin Sylvestre
Missing a character... gotta love it :P
elsurudo
+1  A: 

as for rails3 new syntax, in your controller you should use

@user.company.build

instead of

@user.build_company

apeacox
Actually, this doesn't work. You get a nil reference...
elsurudo
tested it on rails3 beta4. it works, this is an example taken from a controller I wrote:def new @realty = Realty.new @realty.ownerships.build(:person_id => params[:person_id]) respond_to do |format| format.html # new.html.erb endendthe build_* methods was removed for sure.
apeacox
@apeacox It works for you because your @realty has_many ownerships. When it has_one (like in the case of the OP), you need to use @realty.build_ownership
zoopzoop
no, I tested it before answering ;)
apeacox