Mike Kogan
2006-Jul-21 16:27 UTC
[Rails] Using "validates" in Model Question on Control Flow
I started with LoginGenerator and have evolved a nice little ACL model that is perfect for my app. The user model has statements like the following in the model file that control validation of some of the login fields: validates_length_of :login, :within => 3..40 validates_length_of :password, :within => 5..40 validates_presence_of :login, :password, :password_confirmation, :on => :create validates_uniqueness_of :login, :on => :create validates_confirmation_of :password, :on => :create My question is how do I get control when these fields fail with an understanding of which validation clause failed? My "new" view code runs and gets the fields for login then invokes "create" in the controller. In the "create" method I am getting the failure when I call the @user.save method to store the contents. What I really want is to be able to tell the user what he screwed up and return him to a partially filled out form in the "new" view so he can fix the flawed fields. (user name too short, password confirm doesn''t match, etc) Thanks in advance. -- Posted via http://www.ruby-forum.com/.
Guest
2006-Jul-21 21:35 UTC
[Rails] Re: Using "validates" in Model Question on Control Flow
rails provides a generic css file that has error classes that do exactly what you want automatically. The file should be called "scaffold.css". Link to the css file in your "new" view then add this line at the top of your "new" file: <%= error_messages_for(@login) %> Then be sure in your controller.rb file you use render instead of redirect. So it would look something like this: def login_create #create the login record if @login.save #Save was successful else render :action => "login" return false end This tutorial link is for RoR on OSX, but it goes over some of this too: http://developer.apple.com/tools/rubyonrails.html Good luck! -- Posted via http://www.ruby-forum.com/.