Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. ## view, new.rhtml
  2.  
  3. <%= start_form_tag :action => 'create'%>
  4. <%= error_messages_for 'user' %>
  5.  
  6. <p>Screen Name: <%= text_field 'user', 'screen_name' %></p>
  7. <p>Email Address: <%= text_field 'email', 'email' %>
  8. <p>Password: <%= password_field 'user', 'password', :size => 10 %></p>
  9. <p>Confirm Password: <input type="password" name="user[confirm_password]" id ="user_confirm_password" size="10"></p>
  10. <p>Zip Code: <%= text_field 'user', 'zip_code', :size => 5, :maxlength => 5 %></p>
  11. <%= submit_tag 'Create New User' %>
  12. <%= end_form_tag %>
  13.  
  14. ## model, users.rb
  15.  
  16. class User < ActiveRecord::Base
  17. validates_presence_of :screen_name
  18.  
  19. has_many :emails
  20. has_many :logins
  21.  
  22. attr_accessor :password, :confirm_password
  23.  
  24. def validate
  25. errors.add(:password, "not the same") unless password == confirm_password
  26. end
  27.  
  28. def before_save
  29. self.password_sha1 = Digest::SHA256.hexdigest(password) if password
  30. end
  31.  
  32. end
  33.  
  34. ## controller, user_registration_controller.rb
  35.  
  36. class UserRegistrationController < ApplicationController
  37.  
  38. verify :method => :post, :only => [ :destroy, :create, :update ],
  39. :redirect_to => { :action => :list }
  40.  
  41. def new
  42. @user = User.new
  43. @email = Email.new
  44. end
  45.  
  46. def create
  47. @user = User.new(params[:user])
  48. @user.emails << Email.new(params[:email])
  49. if @user.save
  50. flash[:notice] = "New user created successfully"
  51. redirect_to :action => 'whereever'
  52. else
  53. redirect_to :action => 'new'
  54. end
  55. end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement