Rails simple_form server side validations lost URL params

时光怂恿深爱的人放手 提交于 2019-12-03 21:57:04

问题


I'm using simple_form for a form and passing in some URL parameters to prepopulate the form.

This code works OK

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

Using the URL

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

Which outputs this HTML

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

However if the form server side validation fails the page reloads but the prepopulate value is gone? This is the rendered HTML

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

Can anyone help advise how to prepopulate simple_form and retain those values if the serverside validastion fails and the page reloads?

Thank you.


回答1:


if you want to make it works with validations you should preset object values in controller like this:

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


来源:https://stackoverflow.com/questions/11036381/rails-simple-form-server-side-validations-lost-url-params

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!