# Rails form (newbie)

**URL:** <https://www.ruby-forum.com/t/rails-form-newbie/85875>\
**Category:** Rails\
**Created:** [February 17, 2007, 1:53am UTC](https://www.ruby-forum.com/t/rails-form-newbie/85875 "2007-02-17T01:53:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [February 17, 2007, 1:53am UTC](https://www.ruby-forum.com/t/rails-form-newbie/85875/1 "2007-02-17T01:53:27Z")

</div>

Hi,

Still from yesterday trying to get things to work, rails doesn’t seems  
to get things very clear, I made signup.rhtml which looks like this:

\<%= error\_messages\_for :user %\>

\<%= form\_tag :action =\> “signup” %\>

Desired username\<%= text\_field "user", "username", "size" =\> 20 %\>

```
<dt>Email address</dt>
<dd><%= text_field "user", "email", "size" => 20 %></dd>

<dt>Choose a password</dt>
<dd><%= password_field "user", "password", "size" => 20 %></dd>

<dt>Re-enter password</dt>
<dd><%= password_field "user", "confirm", "size" => 20 %></dd>

```

\<%= submit\_tag "Log in", :name=\>"login", :class=\>"submit" %\>\<%= end\_form\_tag %\>

And then I want to validate if username is empty, email is valid email,  
and password is the same as confirm password, but things are not working  
like it should…

here is my controller…

def signup  
if request.post?  
@user = User.new(:username =\> params[:user][:username])  
if @user.save  
redirect\_to :action =\> ‘login’  
else  
@method = ‘not saved’;  
end  
end  
end

This givfes me an error (undefined method `confirm’ for  
#User:0x47f75e8), this looks ugly and doesn’t describe anything to me  
:S:S:S:S

Can somebody help here, rails is giving me too much pain…I thought it  
was more simple then this?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [February 17, 2007, 2:04am UTC](https://www.ruby-forum.com/t/rails-form-newbie/85875/2 "2007-02-17T02:04:34Z")

</div>

On Feb 16, 2007, at 2:53 PM, Jamal S. wrote:

> And then I want to validate if username is empty, email is valid  
> email,  
> and password is the same as confirm password, but things are not  
> working  
> like it should…
> 
> …  
> Can somebody help here, rails is giving me too much pain…I  
> thought it  
> was more simple then this?

Hi Jamal,  
I think you just add a validate method to your class and add the  
error in there if the 2 don’t match.  
I found this code from the rails list archive. Maybe it will help you.

> end  
> end

from  
[http://wrath.rubyonrails.org/pipermail/rails/2004-November/000587.html](http://wrath.rubyonrails.org/pipermail/rails/2004-November/000587.html)

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [February 17, 2007, 7:26am UTC](https://www.ruby-forum.com/t/rails-form-newbie/85875/3 "2007-02-17T07:26:37Z")

</div>

Jamal S. wrote:

> Hi,
> 
> Still from yesterday trying to get things to work, rails doesn’t seems  
> to get things very clear, I made signup.rhtml which looks like this:
> 
> ```
> <dt>Re-enter password</dt>
> <dd><%= password_field "user", "confirm", "size" => 20 %></dd>
> 
> ```
> 
> This givfes me an error (undefined method `confirm’ for  
> #User:0x47f75e8), this looks ugly and doesn’t describe anything to me  
> :S:S:S:S

The form helpers trying access a particular model object to show the  
current value of that object. For instance:

\<%= text\_field ‘post’, ‘title’ %\>

Will try to get “@post.title” as the calue that will load in the text  
field. So when you try to do the above password field, rails tries to  
get the value of “@user.confirm”. The problem is your User class has no  
method or DB field named “confirm”, and it generates a no method error.

To do this do two things

#model  
class User \< ActiveRecord::Base  
#this will give your model a “password\_confirmation” attribute  
validates\_confirmation\_of :password  
end

#view  
\<%= password\_field ‘user’, ‘password\_confirmation’ %\>

> And then I want to validate if username is empty, email is valid email,  
> and password is the same as confirm password, but things are not working  
> like it should…

#model  
class User \< ActiveRecord::Base  
validates\_presence\_of :username  
validates\_format\_of :email, :with =\> /some regex/  
validates\_confirmation\_of :password  
end

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [February 17, 2007, 11:22am UTC](https://www.ruby-forum.com/t/rails-form-newbie/85875/4 "2007-02-17T11:22:54Z")

</div>

Alex W. wrote:

> Jamal S. wrote:
> 
> > Hi,
> > 
> > Still from yesterday trying to get things to work, rails doesn’t seems  
> > to get things very clear, I made signup.rhtml which looks like this:
> 
> Will try to get “@post.title” as the calue that will load in the text  
> field. So when you try to do the above password field, rails tries to  
> get the value of “@user.confirm”. The problem is your User class has no  
> method or DB field named “confirm”, and it generates a no method error.

Thank you very much 🙂 it worked…
