Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #user_controller.rb
  2.  
  3. class UserController < ApplicationController
  4. layout "index"
  5.  
  6. def register
  7.  
  8. @user = User.new(params[:user])
  9.  
  10. if request.post?
  11.  
  12. if @user.save
  13. flash[:notice] = "Thanks for registering."
  14. redirect_to :action => :register
  15. end
  16. end
  17.  
  18. end
  19.  
  20. end
  21.  
  22. # register.rhtml
  23.  
  24. <h2>Register</h2>
  25.  
  26. <p>Please choose a user name and password.</p>
  27.  
  28. <%= start_form_tag :action => "register" %>
  29.  
  30. <%= error_messages_for(:user) %>
  31. <% if @flash[:notice] %><div class="notice"><%= @flash[:notice] %></div><% end %>
  32.  
  33. <label>Username:</label> <%= text_field "user", "username" %> <br />
  34. <label>Password:</label> <%= text_field "user", "password" %> <br />
  35.  
  36. <%= submit_tag "Register" %>
  37.  
  38. <%= end_form_tag %>
  39.  
  40. # user.rb
  41.  
  42. require 'digest/sha2'
  43. class User < ActiveRecord::Base
  44. def password=(pass)
  45. salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
  46. self.password_salt, self.password_hash =
  47. salt, Digest::SHA256.hexdigest(pass salt)
  48. end
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement