将fields_for限制为仅1个新记录

时间:2015-07-03 21:49:31

标签: forms ruby-on-rails-3

我有一个表单,我想为公司添加评论。但我只希望新表单字段显示不是前一个要编辑的字段..

我只想要New Empty字段...而不是之前的所有评论......

控制器:

  def show
    @company_profile = CompanyProfile.find(params[:id])
    1.times do
      @company_profile.company_notes.build
      @company_profile.dispatchers.build
    end
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @company_profile }
    end
  end

查看:

<%= form_for @company_profile, :html => { :class => 'sky-form boxed comments' } do |f| %>
    <fieldset>
      <%= f.fields_for :company_notes do |builder| %>
          <div class="row">
            <div class="col col-md-12">
              <section>
                <label class="input">
                  <%= builder.text_field :notes, :placeholder => "Comment" %>
                  <b class="tooltip tooltip-bottom-right">Enter Comments</b>
                </label>
              </section>
            </div>
          </div>
      <% end %>
    </fieldset>
<% end %>

我只想要新的空场而不是其他2场

enter image description here

为我修复的确切代码,因此您没有额外的行或div

<%= form_for @company_profile, :html => { :class => 'sky-form boxed comments' } do |f| %>
    <fieldset>
      <%= f.fields_for :company_notes do |builder| %>
          <% if builder.object.new_record? %>
          <div class="row">
            <div class="col col-md-12">
              <section>
                <label class="input">
                      <%= builder.text_field :notes, :placeholder => "Comment" %>
                  <b class="tooltip tooltip-bottom-right">Enter Comments</b>
                </label>
              </section>
            </div>
          </div>
          <% end %>
      <% end %>
    </fieldset>
<% end %>

1 个答案:

答案 0 :(得分:1)

以下代码适用于您

angular.module('myApp')
.controller('messageController', ['$scope', '$http', function($scope, $http){
  $http.get()
  .success()
  .error();
}]);