Creating users for Recipes authentication method

If using recipe 31 in Mr. Fowler’s book, one ought really to add a form
for user creation. Something like this (within user_controller.rb),
perhaps:

def register
@user = User.create
if request.post?
@user.attributes = params[:user]
if @user.save
flash[:notice] = “Your request has been saved.”
redirect_to :controller => ‘user’, :action => ‘register_success’,
:user => @user.id and return
else
flash[:error] = “Your request could not be saved.”
redirect_to :controller => ‘user’, :action => ‘register’, :method
=> ‘get’ and return
end
end
end

The way the user model works is that it does not store the user’s
password, but the password_salt and password_hash. The password method
generates these. A means of checking that the password and confirmation
from the form in register.rhtml match, and that the password consists of
the approved characters is needed.

What would be the best place to put this - in the user model or as part
of this register method? If the latter, does anyone have any suggestions
as to a good way to do this?

IMO, validation always belongs in the model.

On Sep 11, 8:14 am, Milo T. [email protected]

Jeff Emminger wrote:

IMO, validation always belongs in the model.

Thanks - I will indeed put it there.

Milo T. wrote:

The way the user model works is that it does not store the user’s
password, but the password_salt and password_hash. The password method
generates these. A means of checking that the password and confirmation
from the form in register.rhtml match, and that the password consists of
the approved characters is needed.

What would be the best place to put this - in the user model or as part
of this register method? If the latter, does anyone have any suggestions
as to a good way to do this?

If you have a chance to pick up the Agile Web D. with Rails
book from the same publisher, there is similar code included in chapter
11. The validation is enforced by the model (via validates_presence_of,
et. al.) while the action of saving and displaying the resulting message
is done in the controller. I’ve been carrying that book around with me
anywhere I go for the last month :slight_smile:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs