Ruby on rails Rails:在尝试提交带有验证错误的表单时防止表单重置
当前,当我提交一个有赋值错误的表单时,该表单被重置为空值。有没有办法将值保留在字段中,这样用户就不必重新输入整个表单 我的创建操作:Ruby on rails Rails:在尝试提交带有验证错误的表单时防止表单重置,ruby-on-rails,Ruby On Rails,当前,当我提交一个有赋值错误的表单时,该表单被重置为空值。有没有办法将值保留在字段中,这样用户就不必重新输入整个表单 我的创建操作: def create @quote = Quote.new(params[:quote]) if @quote.save flash[:success] = "Record Created Successfully" redirect_to quotes_url else render 'new' end end 我的表
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>
试试这个:
在QuotesControllernew中
通过这种方式,您可以将参数失败的@quote实例变量从create传递到“new”模板,而不会用new重写
免责声明:我没有在真实代码中验证这一点。只是想法是的。在我的新动作中有Quote.newparams[:Quote]。显然,这是在覆盖create操作中的params值。谢谢你把我送到正确的方向。
@quote = || Quote.new