1

我正在关注 railcast 嵌套形式

例如:我在注册页面上有 4 个字段,在编辑页面上有 12 个字段(实际上,我需要更多基本信息)

当新客户注册时,他们会在编辑页面上填写所有 12 个字段,并且所有内容都运行良好。

但是,当老客户登录时(在嵌套表单创建之前已经注册)他们现在没有得到所有字段,只得到 4 个字段

我正在使用这样的东西:

控制器

 def new

    @user = User.new
    @user.build_user_nickname_live
    @user.build_user_twitter_chipcount
    @user.user_twitter_reals.build
    @user.user_twitter_fakes.build
end

def edit

     @user = User.find(current_user.id)

  end

模型

has_one :user_nickname_live

accepts_nested_attributes_for :user_nickname_live

has_one :user_twitter_chipcount

accepts_nested_attributes_for :user_twitter_chipcount

has_many :user_twitter_reals, :dependent => :destroy
   accepts_nested_attributes_for :user_twitter_reals,
   :allow_destroy => true

has_many :user_twitter_fakes, :dependent => :destroy
 accepts_nested_attributes_for :user_twitter_fakes,
   :allow_destroy => true

看法

<%= nested_form_for(resource, :as => resource_name,
                    :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>

    <div style="overflow: hidden;display: block;">
      <%= f.fields_for :user_nickname_live do |user_nickname_live| %>
          <!--span 2 and 4 with min-padding-->
          <div class="span2 min-padding">
            <label >Live Nickname</label>
          </div>
          <div class="span4 min-padding">
            <div class="input-control text">
              <%= user_nickname_live.text_field :nickname,:style=>"max-width: 253px;", :placeholder=>"Live nickname",:onchange=>"changeInfo();" %>
            </div>
          </div>
          <!--ends-->
      <% end %>
    </div>
    <div style="overflow: hidden;display: block">
      <%= f.fields_for :user_twitter_chipcount do |user_twitter_chipcount| %>
          <!--span 2 and 4 with min-padding-->
          <div class="span2 min-padding">
            <label >Chip count account</label>
          </div>
          <div class="span4 min-padding">
            <div class="input-control text">
              <%= user_twitter_chipcount.text_field :twitter_url,:style=>" max-width: 250px;", :id=>"twitter_url", :placeholder=>"Chip count account" ,:onchange=>"changeInfo();" %>
            </div>
          </div>
          <!--ends-->
      <% end %>
    </div>

    <div id="real-one" style="display:block;position: relative;">
      <div class="add-click">
        <%= f.link_to_add "", :user_twitter_reals, :class=>"icon-plus",:style=>"font-size: 14px;", :data => { :target => "#real-field" } %>
      </div>
      <%= f.fields_for :user_twitter_reals do |real_one| %>
          <div id="real-field"  style="overflow: hidden;">
            <!--span 2 and 4 with min-padding-->
            <div class="span2 min-padding">
              <label >Real</label>
            </div>
            <div class="span4 min-padding">
              <div class="input-control text" >
                <%= real_one.text_field :twitter_url,:style=>"max-width: 250px;",:placeholder=>"Real",:onchange=>"changeInfo();" %>
                <%= real_one.link_to_remove "", :class=>"icon-minus minus-click" %>
              </div>
            </div>
            <!--ends-->

          </div>
      <% end %>
    </div>

    <div id="twitter-fake" style="display: block;position: relative;">
      <div class="add-click">
        <%= f.link_to_add "", :user_twitter_fakes, :class=>"icon-plus",:style=>"font-size: 14px;", :data => { :target => "#fake-field" } %>
      </div>
      <%= f.fields_for :user_twitter_fakes  do |user_twitter_fake| %>

          <div id="fake-field"  style="overflow: hidden;">
            <!--span 2 and 4 with min-padding-->
            <div class="span2 min-padding">
              <label >Fake</label>
            </div>
            <div class="span4 min-padding">
              <div class="input-control text">
                <%= user_twitter_fake.text_field :twitter_url,:style=>"max-width: 250px;",:placeholder=>"Fake",:onchange=>"changeInfo();" %>
                <%= user_twitter_fake.link_to_remove "", :class=>"icon-minus minus-click" %>
              </div>
            </div>
            <!--ends-->
          </div>
      <% end %>
    </div>
<% end %>

请帮帮我。

4

1 回答 1

0

当时创建这些关联的旧用户不存在。

因此,如果老用户现在想要编辑其个人资料,例如:

<%= f.fields_for :user_nickname_live do |user_nickname_live| %>

无法显示任何内容,因为该关联尚未在您的数据库中建立。

您可以创建迁移、rake 任务或后台任务或在 irb 中修复它以添加旧用户用户的关联

我没有尝试过,但它应该可以工作。创建一个迁移调用它,例如: build_user_nickname_live_associations 并放入:

def up
  users = User.find(:all)

  users.each do |u|
    unless u.nick_name_live.present?
      u.build_nick_name_live
      u.save
    end
  end
end
于 2013-07-23T08:30:44.700 回答