2

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.

Rudi Starcevic
  • 645
  • 1
  • 11
  • 17

1 Answers1

2

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]
Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
  • Great! Many thanks Nash, works perfect for simple_form value populating both before initial submit and after any failed serverside validations. – Rudi Starcevic Jun 15 '12 at 02:42