Forum: Ruby on Rails validate problem

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
0c4473471a3dd5749fa3560b7e9c0d68?d=identicon&s=25 Whole Blackberry (wholeblackberry)
on 2007-07-26 15:44
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", but seems the validation
of first name and last name doesn't work. Why?

the code like below:

class User < ActiveRecord::Base
  has_one :profile
  validates_presence_of :password,:on => :create
  validates_format_of :email:with =>
  validates_uniqueness_of :email, :on => :create

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"

--------- 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

  def home
    render :layout => 'home'

  def editprofile
     unless current_user.profile.nil?
      @profile = current_user.profile

  def updateprofile
    if current_user.profile.nil?
      @profile =[:profile])
      @profile.user = current_user
      @profile = current_user.profile
       unless @profile.update_attributes(params[:profile])


----code end---

The view for updating profile is like below:
<%= error_message_for 'profile'%>
% form_for :profile,@profile,
            :url => {:action => "updateprofile",:i=>@params["i"]},
            :html => {:class => 'required-validate'} do|form|  %>
      <div class="titlebg1 strong" style="
background:url(/images/title_step1.gif) no-repeat top left; ">Step 1.
Your Personal Contact Information</div>
<table width="100%" border="0" cellpadding="5" cellspacing="1"
        <td width="100%" valign="top" bgcolor="#f1f1f1"><table
width="726" border="0" cellspacing="0" cellpadding="2">
            <td colspan="3" class="h3">What's your name?</td>
            <td>First Name:</td>
            <td width="155"><%= form.text_field :firstname, :class =>
'required' %></td>
            <td>Last Name: <%= form.text_field :lastname, :class =>
'required' %></td>
<% 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.
This topic is locked and can not be replied to.