# Two quick newbie questions

**URL:** <https://www.ruby-forum.com/t/two-quick-newbie-questions/76577>\
**Category:** Rails\
**Created:** [March 4, 2006, 7:01am UTC](https://www.ruby-forum.com/t/two-quick-newbie-questions/76577 "2006-03-04T07:01:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 4, 2006, 7:01am UTC](https://www.ruby-forum.com/t/two-quick-newbie-questions/76577/1 "2006-03-04T07:01:55Z")

</div>

2 quick questions regarding authentication …

1. the flash[:notice] on successful login looks completely wrong to me.  
How should it be done?

def index  
if request.post?  
@user = User.new(params[:user])  
authentic\_user = @user.attempt\_login  
if authentic\_user  
session[:user\_id] = authentic\_user.id  
flash[:notice] =  
'Login successful! Welcome ’ + authentic\_user.first\_name + ’ ’ +  
authentic\_user.last\_name + ‘!’  
redirect\_to(:controller =\> ‘user’)  
else  
flash[:notice] = ‘Invalid username or password.’  
end  
end  
end

1. can someone explain why the first of these two techniques works (ie,  
login is successful) but the second doesn’t:

@user = User.new(params[:user])  
@user = User.new(params[:user =\> ‘username’, :user =\> ‘password’])

… given this model:

class User \< ActiveRecord::Base  
attr\_accessor :password  
attr\_accessible :username, :password

def before\_create  
self.hashed\_password = User.encrypt\_password(self.password)  
end

def after\_create  
@password = nil  
end

def self.login(username, password)  
hashed\_password = encrypt\_password(password || ‘’)  
find(:first,  
:conditions =\> [‘username = ? and hashed\_password =?’,  
username, hashed\_password])  
end

def attempt\_login  
User.login(self.username, self.password)  
end

private  
def self.encrypt\_password(password)  
Digest::SHA1.hexdigest(password)  
end

end

… and assuming a form with fields ‘name=user[username]’ &  
‘name=user[password]’. Thanks for the help. Kindly appreciated.

Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 4, 2006, 9:50am UTC](https://www.ruby-forum.com/t/two-quick-newbie-questions/76577/2 "2006-03-04T09:50:14Z")

</div>

irb(main):001:0\> “XXX” + “123”  
=\> “XXX123”

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 4, 2006, 9:44am UTC](https://www.ruby-forum.com/t/two-quick-newbie-questions/76577/3 "2006-03-04T09:44:13Z")

</div>

for the 1st question:

I’m fairly new to rails too, but i dont think thats how you concatenate  
strings in ruby. I think you need “\<\<” where the “+” is.

for the 2nd question:

params is a hash, and you are referencing the values to the key “user”.  
The  
second one looks wrong because it seems to be redefining the key “user”  
to  
two different things, username and password.

When you do User.new(params[:user]), you are passing the hash to the  
model  
to build a new user object. This would be the same as  
User.new(:username =\>  
“foo”, :password =\> “secret”, …)

the call to new is also given a hash.

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 4, 2006, 9:53am UTC](https://www.ruby-forum.com/t/two-quick-newbie-questions/76577/4 "2006-03-04T09:53:14Z")

</div>

this is what you want:

user = User.new(:username =\> “the username”, :password =\> “the  
password”)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [March 4, 2006, 9:56am UTC](https://www.ruby-forum.com/t/two-quick-newbie-questions/76577/5 "2006-03-04T09:56:15Z")

</div>

try that instead:

flash[:notice] = ‘Login successful! Welcome  
#{authentic\_user.first\_name}  
#{authentic\_user.last\_name}!’

one of the things why I love ruby…
