Forum: Ruby on Rails rails form (newbie)

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.
Jamal S. (Guest)
on 2007-02-17 02:53
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" %>
<dl>
    <dt>Desired username</dt>
    <dd><%= text_field "user", "username", "size" => 20 %></dd>

    <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>

   <dt></dt>
   <dd><%= submit_tag "Log in", :name=>"login", :class=>"submit" %>
</dd>

</dl>
<%= 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?
Andy T. (Guest)
on 2007-02-17 03:04
(Received via mailing list)
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-...
Alex W. (Guest)
on 2007-02-17 08:26
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
Jamal S. (Guest)
on 2007-02-17 12:22
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...
This topic is locked and can not be replied to.