HI, I am new to ruby. I am developing small application in ROR. Following is code for my rHtml , Controller & Model. PLease see it & let me know why data in form is not get validated & also how to check for whether data i have entered in form (username) is present in database or not? (Like Login form). following is my code:- ---index.rhtml-------- <%= start_form_tag(:action => "login")%> <%=text_field("user", "name", "size" => 20)%> <%=password_field("user", "password", "size" => 20)%> <%= submit_tag %> <%= end_form_tag %> ------------------------------------------ -----users_controller.rb----------------- def login @user = User.new(params[:user]) redirect_to :action => ''list'' end def new @user = User.new end ----------------------------------------- ------user.rb---------------------------- class User < ActiveRecord::Base validates_presence_of :name end ----------------------------------------- In Above Form I want when user enters "name" field blank ihn form it should give error message of validation. Thanks in advance. Prash -- Posted via http://www.ruby-forum.com/.
On Fri, 2006-03-17 at 05:44 +0100, Prashant Tiwari wrote:> HI, > I am new to ruby. I am developing small application in ROR. Following is > code for my rHtml , Controller & Model. PLease see it & let me know why > data in form is not get validated & also how to check for whether data i > have entered in form (username) is present in database or not? (Like > Login form). > following is my code:- > ---index.rhtml-------- > <%= start_form_tag(:action => "login")%> > <%=text_field("user", "name", "size" => 20)%> > <%=password_field("user", "password", "size" => 20)%> > <%= submit_tag %> > <%= end_form_tag %> > ------------------------------------------ > -----users_controller.rb----------------- > def login > @user = User.new(params[:user]) > redirect_to :action => ''list'' > end > > def new > @user = User.new > end > ----------------------------------------- > ------user.rb---------------------------- > class User < ActiveRecord::Base > validates_presence_of :name > end > ----------------------------------------- > > In Above Form I want when user enters "name" field blank ihn form it > should give error message of validation. >---- I don''t see any problem there...that looks good. If there''s a problem (and you didn''t say there is), the problem is likely how you handle errors in your controller... and of course, I don''t know what new.rhtml looks like. Craig
The problem is that you haven''t asked if it is valid: def login @user = User.new(params[:user]) if @user.valid? redirect_to :action => ''list'' return # cuz you can only redirect once per action else flash[:error] = ''something bad happened'' render :action => ''index'' end end My (untested) code is dismal because neither code path displays a natural template (which would be new.rhtml) relying on redirects or renders instead. Consider using a before_filter to do your login as described nicely in AWDWR. If you''d like to see some straightforward Rails code, do a ''script/generate scaffold MyTable'' and you''ll see how the paradigm works with new/create and edit/update. -- View this message in context: http://www.nabble.com/Validation-not-working.......-t1295810.html#a3450502 Sent from the RubyOnRails Users forum at Nabble.com.