Cannot register a new user

Hello.. I wanted to extend my webpage's user interaction by allowing users to register themselves. The account controller method "register" takes care of the process. This is the register.rhtml code i wrote in apps/views/accounts/register.rhtml :

<% form_for :user do |f| %> <p>     login:<br />     <%= f.text_field :login %> </p> <p>     password:<br />     <%= f.password_field :password %> </p> <p>     name:<br />     <%= f.text_field :name %> </p> <p>     email:<br />     <%= f.text_field :email %> </p> <p><%= submit_tag %></p> <% end %>

The register method in the account controller looks like :

def register      @u = User.new(params[:login])      @u = @current_user      if request.post? and @u.save        @current_user = User.new(params[:login])         flash[:notice] = 'Registration succeeded'         redirect_to :controller => 'story', :action => 'index'     end   end

The problem it gives a nil object error for the statement @u.save . I donot understand since when im breakpointing ...$params shows that it holds the entire submitted form values ...but $params[:login] => nil $params[:name] => nil

where is the all the data going and why is it not getting saved. please help

first you do:

 @u = User\.new\(params\[:login\]\)

then you overwrite @u with:

 @u = @current\_user

but @current_user is set later:

   @current\_user = User\.new\(params\[:login\]\)

so most likely @u = @current_user sets @u to nil, since @current_user isn't set before that. In any case you would lose the newly created user.

And you should NEVER use variable names like @u that do not tell what they are good for...

Thorsten Mueller wrote:

first you do:

� � �@u = User.new(params[:login])

then you overwrite @u with:

� � �@u = @current_user

but @current_user is set later:

� � � �@current_user = User.new(params[:login])

so most likely @u = @current_user sets @u to nil, since @current_user isn't set before that. In any case you would lose the newly created user.

And you should NEVER use variable names like @u that do not tell what they are good for...

Thorsten.. was getting bugged rewriting the name .. so was using "u" .. i know its a bad habit.. so changed it..

anyways .. i applied the changes and the story saves..thanks.. but all the fields are being set to null. i mean the values the user enters for login password in registration page are all being set to null instead of what was enterd ... weird .. eh ?? is their a prob with the form_for object ?

no, there is no problem with form_for but you're using params[:login] instead of params[:user] a form like this has a params hash named as the model the form is made for the single fields are used then like: params[:user][:login] in your case User.new(params[:user]) should work

Thanks.. its working :slight_smile: yes i guess i was hung up on login than the actual object name "user"

User.find_by_login(params[:login]) should also work fine then !

User.find_by_login(params[:login]) should also work fine then !

If it has it's own form then maybe. But more likely you have a form with some name and some fields.

so if the forms name is login and the fieldname is username then User.find_by_login(params[:login][:username])

or if the form is named user again and the textfield is login, then: User.find_by_login(params[:user][:login])

because find_by_login expects the single string, while User.new expects a hash with all the users attributes.