开发者

Why doesn't my nested form display?

开发者 https://www.devze.com 2023-03-12 20:51 出处:网络
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.

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_fie开发者_开发技巧ld :email %>
          <span>Note: Your email will not be publicly displayed</span>
        <% end %>
      </li>
....


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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消