# Validate problem

**URL:** <https://www.ruby-forum.com/t/validate-problem/110458>\
**Category:** Rails\
**Created:** [July 26, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/validate-problem/110458 "2007-07-26T15:44:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wholeblackberry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/9fc348/32.png) [@wholeblackberry](https://www.ruby-forum.com/u/wholeblackberry)\
**Post date:** [July 26, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/validate-problem/110458/1 "2007-07-26T15:44:56Z")

</div>

Seems the validation can’t work good!  
I have 2 models: user and profile. a user is with a profile, there are  
stored in different tables in database,(user is in  
users(user\_id,email,password,…),profile is  
profiles(firstname,lastname,user\_id,…). and all operations are  
implement in UserController. I want to edit and update the profile, the  
URL like “[http://yourdomain/users/editprofile](http://yourdomain/users/editprofile)”, but seems the validation  
of first name and last name doesn’t work. Why?

the code like below:

----------------code--------------------  
class User \< ActiveRecord::Base  
has\_one :profile  
validates\_presence\_of :password,:on =\> :create  
validates\_format\_of 📧with =\>  
/\A([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})\Z/i  
validates\_uniqueness\_of :email, :on =\> :create  
end

class Profile \< ActiveRecord::Base  
belongs\_to :user  
validates\_presence\_of :firstname,:message=\>“First name is required”  
validates\_presence\_of :lastname,:message=\>“Last name is required”  
end

--------- code end-------

a user has a profile, and in the database, profiles table has a user\_id.

there is no controller for Profile model, and all operations are  
included in user\_controller, the operations include singup, edit  
profile, update profile.

UserController like below:  
-----UserController Code–  
class UsersController \< ApplicationController  
before\_filter :login\_required, :only =\> [:home, :editprofile,  
:updateprofile, :changepass, :getloan, :createdealing, :history]  
def index  
end

def home  
render :layout =\> ‘home’  
end

def editprofile  
unless current\_user.profile.nil?  
@profile = current\_user.profile  
end  
…  
end

def updateprofile  
if current\_user.profile.nil?  
@profile = Profile.new(params[:profile])  
@profile.user = current\_user  
begin  
@profile.save!  
rescue  
…  
end  
else  
@profile = current\_user.profile  
unless @profile.update\_attributes(params[:profile])

```
  end
.....

```

end

…  
----code end—

The view for updating profile is like below:  
------------/users/editprofile.rhtml----  
\<%= error\_message\_for ‘profile’%\>  
% form\_for :profile,@profile,  
:url =\> {:action =\> “updateprofile”,:i=\>@params[“i”]},  
:html =\> {:class =\> ‘required-validate’} do|form| %\>

Step 1.  
Your Personal Contact Information

... \<% end %\>

------end---------

Here, first name and last name is required. In updateprofile  
function,@profile.update\_attributes(params[:profile]) will be failure,  
if the first name or last name is empty. but why it doesn’t give any  
errors in view page.\<%= error\_message\_for ‘profile’%\> doesn’t work!

please advice.

| 

| What's your name? |
| First Name: | \<%= form.text\_field :firstname, :class =\> 'required' %\> | Last Name: \<%= form.text\_field :lastname, :class =\> 'required' %\> |

 |
