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:
----------------code--------------------
class User < ActiveRecord::Base
has_one :profile
validates_presence_of :password,:on => :create
validates_format_of :email: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| %>
<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"
bgcolor="#CCCCCC">
<tbody>
<tr>
<td width="100%" valign="top"
bgcolor="#f1f1f1"><table
width="726" border="0" cellspacing="0"
cellpadding="2">
<tr>
<td colspan="3" class="h3">What''s
your name?</td>
</tr>
<tr>
<td>First Name:</td>
<td width="155"><%= form.text_field :firstname,
:class =>
''required'' %></td>
<td>Last Name: <%= form.text_field :lastname, :class =>
''required'' %></td>
</tr>
</table>
</td>
</tr>
...
<% 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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---