【问题标题】:Rails simple_form server side validations lost URL paramsRails simple_form 服务器端验证丢失 URL 参数
【发布时间】:2012-06-14 15:27:02
【问题描述】:

我将 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 并保留这些 如果服务器端验证失败并且页面重新加载,则值?

谢谢。

【问题讨论】:

标签: simple-form


【解决方案1】:

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

@charity = Charity.new
@charity.first_name = params[:first]

【讨论】:

  • 太棒了!非常感谢 Nash,非常适合在初始提交之前和任何失败的服务器端验证之后填充 simple_form 值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-25
  • 2022-12-03
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多