Validates_presence_of and a blank form


#1

Hey All,

I’m frustrated with validates_presence_of. I have a form, and I’m
validating four fields using validates_presence_of…

It works fine, except that when the form comes back with the error
messages, it is blank. If a user enters three of the four required
fields and submits the form, they get back a blank form with an error
message and have to re-enter everything.

It seems to me like there must be an easy way to fix this, otherwise
v_p_o is a total joke.

I’ve googled my head off to no good effect. Can anyone offer advice?

Thanks,
JB


#2

One way to make this happen is to do a redirect in the controller after
Model.save or Model.update_attributes rather than a render back to the
new or edit action. Could that be your problem?

(If that’s not clear, post the update method of the appropriate
controller.)

HTH,

-Roy


#3

To add to the previous post…make sure that you label you form
correctly, for example:
<% form_for(@person) do |f| %>
<%= f.error_messages %>

<%= f.label :name%>
<%= f.text_field :name %>

<%= f.submit "Create" %>

<% end %>

in the code above, if the @person has a name, the text_field is then
populated with that value. or what you could do is get the choice made
by the user i.e.: <% selected_choice =
@survey.get_choice_for_question(@question) %>
end if it’s not nil display it as follows: <% selected_choice_id =
selected_choice.id unless selected_choice.nil? %>

(i have a random number of questions so i use a loop to display each
one and get the appropriate answers and such so you have to adapt my
code to what you have)

I had the same problem and this fixed it for me, so i hope it helps.
if not, show us your code and we’ll see how we can help.