Forum: Ruby on Rails Validation not working.......

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Prashant T. (Guest)
on 2006-03-17 06:44
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
Craig W. (Guest)
on 2006-03-17 07:09
(Received via mailing list)
On Fri, 2006-03-17 at 05:44 +0100, Prashant T. wrote:
> <%=password_field("user", "password", "size" => 20)%>
>     @user = User.new
>
----
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
Steve R. (Guest)
on 2006-03-17 07:42
(Received via mailing list)
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.........
Sent from the RubyOnRails Users forum at Nabble.com.
This topic is locked and can not be replied to.