Forum: Ruby on Rails validation errors not being displayed

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.
miriamraphael (Guest)
on 2005-11-30 15:52
(Received via mailing list)
I can't figure out why my validation errors are not showing up when I
submit my form with errors.
  My new.rhtml view looks like:

<% unless @user.errors.empty? %>
       <p>Your comment could not be saved because:</p>
       <ul>
          <% @user.errors.each_full do |msg| %>
            <li><%= msg %></li>
          <%  end %>
       </ul>
    <% end %>
  <form action="/user/create" method="post">
<table width=400>
<tr bgcolor="#999999"><td colspan=2>Please choose a username and
password to create your account</tr>
  <tr><td>User Name*:</td><td><input id="username" name="user[username]"
maxlength=12 type=text></tr>
<tr><td>Password*:</td><td><input id="password" name="user[password]"
maxlength=12 type=password></tr>
  <tr bgcolor="#999999"><td colspan=2>Account Information</td></tr>
  <tr><td>Company Name*:</td><td><input id="company_name"
name="user[company_name]" maxlength=25 type=text></td></tr>
<tr><td>First Name*:</td><td><input id="first_name"
name="user[first_name]" maxlength=25 type=text></td></tr>
<tr><td>Last Name*:</td><td><input id="last_name" name="user[last_name]"
maxlength=25 type=text></td></tr>
<tr><td>Email Address*:</td><td><input id="email" name="user[email]"
maxlength=35 type=text></td></tr>
<tr><td>Address 1*:</td><td><input id="address1" name="user[address1]"
maxlength=40 type=text></td></tr>
<tr><td>Address 2:</td><td><input id="address2" name="user[address2]"
maxlength=40 type=text></td></tr>
<tr><td>City*:</td><td><input id="city" name="user[city]" maxlength=35
type=text></td></tr>
<tr><td>State:</td>
<td><select name="user[state]">
 <% @mystates.each do |state| %>
 <option value="<%= state.statename %>">
 <%= state.statename %>
 </option>
 <% end %>
 </select>
</td></tr>
<tr><td>Zip Code*:</td><td><input id="zip" name="user[zip]" maxlength=12
type=text></td></tr>
<tr><td>Phone*:</td><td><input id="phone" name="user[phone]"
maxlength=15 type=text></td></tr>
<tr><td>Mobile*:</td><td><input id="mobile" name="user[mobile]"
maxlength=15 type=text></td></tr>
<tr><td colspan=2><input type="submit" value= "Save and
Continue"></td></tr>
</form>
</table>

  My Model looks like:

validates_length_of :username, :within =>6..12, :message=>"Username must
be at least 10 characters"
validates_presence_of :username, :password, :company_name, :first_name,
:last_name, :email, :address1, :address2, :city, :zip, :phone
validates_uniqueness_of :username

And my controller looks like:

class UserController < ApplicationController
ActiveRecord::Validations::ClassMethods
 scaffold :user

 def new
 @user = User.new
 @mystates = State.find_all
 @user.errors
 end
  def create
   @newuser = User.new(@params['user'])
    @newuser.creation_date = Date.today
    @newuser.last_login = Date.today
    if @newuser.save
       flash[:notice] = "Entry was successfully created"
       redirect_to :action => "new"
     else
       render_action "new"
     end
    end
  end

I also tried inserting "<%= error_messages_for 'user' %>" into my view,
but that didn't work either. What's happening now is if i submit the
form with an 'errors', the new view/template reappears without any
errors. I want it to reappear with errors (and also preferably with the
user's info defaulted so that they don't have to retype it in).
I've already spent two hours trying to get this to work :( Any help will
be much appreciated!
codyfauser (Guest)
on 2005-11-30 17:34
(Received via mailing list)
Miriam,

Name your user object in both the new and create actions to @user.
You're looking for errors on the @user instance variable , but you're
creating a @newuser instance variable in your action.

Hope this helps.


Cody

On 11/30/05, Miriam P. Raphael <removed_email_address@domain.invalid> wrote:
>        </ul>
> <tr><td>Company Name*:</td><td><input id="company_name"
> maxlength=40 type=text></td></tr>
> </td></tr>
> My Model looks like:
> ActiveRecord::Validations::ClassMethods
>     @newuser.last_login = Date.today
> that didn't work either. What's happening now is if i submit the form with
>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>


--
http://www.codyfauser.com
This topic is locked and can not be replied to.