Forum: Ruby on Rails validates_presence_of and a blank form...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
justinbankston (Guest)
on 2008-10-09 22:25
(Received via mailing list)
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
Roy P. (Guest)
on 2008-10-09 23:07
(Received via mailing list)
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
Youssef Chaker (Guest)
on 2008-10-11 03:42
(Received via mailing list)
To add to the previous post...make sure that you label you form
correctly, for example:
<% form_for(@person) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name%><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.submit "Create" %>
  </p>
<% 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.
This topic is locked and can not be replied to.