目前,当我提交有验证错误的表单时,表单会重置为空白值。有什么方法可以保留字段中的值,以便用户不必重新输入整个表单?
我的创建动作:
def create
@quote = Quote.new(params[:quote])
if @quote.save
flash[:success] = "Record Created Successfully"
redirect_to quotes_url
else
render 'new'
end
end
我的表格:
<%= form_for @quote do |f| %>
<fieldset>
<% if @quote.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@quote.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% @quote.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :quote_date, "Date of Quote" %> <br/>
<%= f.text_field :quote_date %>
</p>
<p>
<%= f.label :good_through %> <br/>
<%= f.text_field :good_through %>
</p>
<p>
<%= f.label :quote_number %><br/>
<%= f.text_field :quote_number %>
</p>
<p>
<%= f.label :customer_id, "Customer" %><br/>
<%= select(:quote, :customer_id, Customer.all.collect {|c| [ c.fname, c.id ] }, :prompt => "Select Customer") %>
</p>
<%= f.fields_for :quote_items do |builder| %>
<%= render 'quote_item_fields', :f => builder %>
<% end %>
<%= link_to_add_fields "Add Line Item", f, :quote_items %>
<p>
<%= f.submit %>
</p>
</fieldset>