FeaturesPluginsDocs & SupportCommunityPartners

NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Beginner/ How to add an email field

 
Post new topic   Reply to topic    NetBeans Forums -> Ruby Users
View previous topic :: View next topic  
Author Message
fa00026



Joined: 20 Feb 2009
Posts: 29

PostPosted: Sun Apr 12, 2009 10:00 pm    Post subject: Beginner/ How to add an email field Reply with quote

Hello,
I am developing a forum, when a user registers himself he will enter a name, password and an email.

I just need some guidance regarding how to add the email field, of course including the validation.

this is my model
class User < ActiveRecord::Base

validates_presence_of :name
validates_uniqueness_of :name

attr_accessor :password_confirmation
validates_confirmation_of :password

validate :password_non_blank




def self.authenticate(name, password)
user = self.find_by_name(name)
if user
expected_password = encrypted_password(password, user.salt)
if user.hashed_password != expected_password
user = nil
end
end
user
end
# 'password' is a virtual attribute
def password
@password
end

def password=(pwd)
@password = pwd
return if pwd.blank?
create_new_salt
self.hashed_password = User.encrypted_password(self.password, self.salt)
end


private

def password_non_blank
errors.add(:password, "Missing password") if hashed_password.blank?
end

def create_new_salt
self.salt = self.object_id.to_s + rand.to_s
end

def self.encrypted_password(password, salt)
string_to_hash = password + "wibble" + salt
Digest::SHA1.hexdigest(string_to_hash)
end
end

This is my controller

class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.find(:all, :order => :name)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end

# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/new
# GET /users/new.xml
def new
@user = User.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end

# GET /users/1/edit
def edit
@user = User.find(params[:id])
end

# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])

respond_to do |format|
if @user.save
flash[:notice] = "User #{@user.name} was successfully created."
format.html { redirect_to(:action=>'index') }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])

respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = "User #{@user.name} was successfully updated."
format.html { redirect_to(:action=>'index') }
format.xml { head Surprisedk }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy

respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head Surprisedk }
end
end
end


this is the edit view[/u]

<h1>Editing users</h1>

<% form_for(@user) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :hashed_password %><br />
<%= f.text_field :hashed_password %>
</p>
<p>
<%= f.label :salt %><br />
<%= f.text_field :salt %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>

This is the new view
<div class="depot-form">

<% form_for(@user) do |f| %>
<%= f.error_messages %>

<fieldset>
<legend>Enter Your Details Please</legend>

<div>
<%= f.label :name %>:
<%= f.text_field :name, :size =>40 %>
</div>

<div>
<%= f.label :user_password, 'Password' %>:
<%= f.password_field :password, :size => 40 %>
</div>

<div>
<%= f.label :user_password_confirmation, 'Confirm' %>:
<%= f.password_field :password_confirmation, :size =>40 %>
</div>

<div>
<%= f.submit "Register me", :class => "submit" %>
</div>

</fieldset>
<% end %>

</div>

And the show view


<p>
<b>Name:</b>
<%=h @user.name %>
</p>

<p>
<b>Hashed password:</b>
<%=h @user.hashed_password %>
</p>

<p>
<b>Salt:</b>
<%=h @user.salt %>
</p>


<%= link_to 'Edit', edit_user_path(@user) %> |
<%= link_to 'Back', users_path %>

What changes shall I add!
Thanks a lot
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Ruby Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB