【发布时间】:2017-04-13 13:20:03
【问题描述】:
我的Locations#show 视图上有一个用于不同型号 (PotentialClient) 的表单。当我的表单验证失败时,重定向会清空所有字段,因为我需要初始化一个新的 potential_client 以加载视图。
如何更改此设置,以便在验证失败后填充字段?
# LocationsController
def show
@potential_client = PotentialClient.new
end
# class PotentialClient < ActiveRecord::Base
validates_presence_of :name, :email, :phone
# PotentialClientsController
def create
@potential_client = PotentialClient.new(potential_client_params)
respond_to do |format|
if @potential_client.save
format.html { redirect_to Location.find(@potential_client.location_id), notice: 'Success!' }
else
format.html { redirect_to Location.find(@potential_client.location_id), notice: 'Failure!' }
end
end
end
# Form in /locations/show
<%= simple_form_for [ @location, @potential_client ] do |f| %>
<%= f.input :name %>
<%= f.input :email %>
<%= f.label :phone %>
<%= f.input :message %>
<%= f.hidden_field :location_id, value: @location.id %>
<%= f.submit "Submit" %>
<% end %>
【问题讨论】:
标签: ruby-on-rails validation ruby-on-rails-4