Rails form (newbie)


#1

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?


#2

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


#3

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


#4

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 :slight_smile: it worked…