将 fields_for 限制为仅 1 条新记录
Limit fields_for to only 1 New Record
我有一个要向公司添加评论的表格。但我只希望新表单字段显示而不是前一个要编辑的字段..
我只想要新的空白字段...不是所有以前的评论...
控制器:
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 个
为我修复的确切代码,因此您没有额外的行或 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 %>
以下代码适合您
<% if builder.object.new_record? %>
<%= builder.text_field :notes, :placeholder => "Comment" %>
<% end %>
我有一个要向公司添加评论的表格。但我只希望新表单字段显示而不是前一个要编辑的字段..
我只想要新的空白字段...不是所有以前的评论...
控制器:
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 个
为我修复的确切代码,因此您没有额外的行或 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 %>
以下代码适合您
<% if builder.object.new_record? %>
<%= builder.text_field :notes, :placeholder => "Comment" %>
<% end %>