2

我将 simple_form 用于表单并传入一些 URL 参数来预填充表单。

此代码工作正常

<%= f.input :first_name, :label => 'First Name', :input_html => { :value => params['first'] } %>

使用网址

http://localhost:3000/charities/new?first=Bob

输出这个 HTML

<input class="string required" id="charity_first_name" name="charity[first_name]" size="50" type="text" value="Bob" />

但是,如果表单服务器端验证失败,页面会重新加载但预填充值消失了?这是呈现的 HTML

<input class="string required" id="charity_first_name" name="charity[first_name]" size="50" type="text" />

如果服务器端验证失败并且页面重新加载,谁能帮助建议如何预填充 simple_form 并保留这些值?

谢谢你。

4

1 回答 1

2

如果你想让它与验证一起工作,你应该在控制器中预设对象值,如下所示:

@charity = Charity.new
@charity.first_name = params[:first]
于 2012-06-14T19:34:07.097 回答