1

Running on Rails 3.1 RC1 and following this.

A user can have one or many emails. My email fields don't show in the form. No error is rendered.

The form element is displayed. But no input fields. Nada. Am I missing something?

Controller:

def new
  @user = current_user

  #1.times do
    email = @user.emails.build
  #end
....
end

User model:

class User < ActiveRecord::Base
  has_many :emails, :dependent => :destroy
  accepts_nested_attributes_for :emails
....
end

Email model:

class Email < ActiveRecord::Base
  belongs_to :user
end

Form:

<%= form_for(@user, :url => users_path, :html => { :method => :post }) do |f| %>

    <ul>

      <li class="clearfix">
        <%= f.fields_for :emails do |builder| %>
          <%= builder.label :email %>
          <%= builder.text_field :email %>
          <span>Note: Your email will not be publicly displayed</span>
        <% end %>
      </li>
....
1
  • Deleted my answer. I didn't notice it was for Rails 3.1. Maybe that is a new way of rendering out fields for multiple objects. Commented Jun 13, 2011 at 0:48

1 Answer 1

4

Looks like the fields_for syntax has changed in Rails 3.1 RC, as per this quote:


I can confirm this. It seems the header of fields_for is changed is Rails 3.1rc:

Ruby on Rails latest stable (v3.0.7):

 fields_for(record_or_name_or_array, *args, &block)

Ruby on Rails (v3.1rc):

 fields_for(record_name, record_object = nil, options = {}, &block)

Works now with the following:

    <%= f.fields_for :emails, :emails do |builder| %>
      <%= builder.label :email %>
      <%= builder.text_field :email %>
      <span>Note: Your email will not be publicly displayed</span>
    <% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.