Ruby practice-create and modify a user with password encryption

Source: Internet
Author: User

Development Environment

RUBY: ruby1.9.1

Rails: rails2.3.5

MySQL: mysql5.0.9

Driver: mysql-2.8.1-x86-mingw32.gem

IDE: rubymine2.0.1

 

1. Create a data table users

Use the scaffold tool that comes with rubymine to create a data table users. You can also manually create

 

2. Create controller and view

Ruby project-> right-click-> Create model

The corresponding file is automatically generated

 

3. Modify model user. Rb

Use digest/sha1 to encrypt and save passwords

The modified code is as follows:

Require "Digest/sha1" </P> <p> class user <activerecord: Base <br/> attr_accessor: hashed_password,: repassword <br/> attr_accessible: username ,: hashed_password,: repassword <br/> validates_uniqueness_of: username <br/> validates_presence_of: username,: hashed_password </P> <p> def before_create <br/> self. password = user. hash_password (self. hashed_password) <br/> end </P> <p> def after_create <br/> @ hashed_password = nil <br/> end </P> <p> def before_update <br/> self. password = user. hash_password (self. hashed_password) <br/> end </P> <p> def after_update <br/> @ hashed_password = nil <br/> end </P> <p> private </P> <p> def self. hash_password (hashed_password) <br/> Digest: sha1.hexdigest (hashed_password) <br/> end </P> <p> end

 

4. Modify users_controller.rb

Modify the update method to determine whether the password and password_confirm are consistent during the edit operation,

If the password is the same, the update operation is performed. Otherwise, the system prompts that the user password is inconsistent with the password_confirm input.

The modified code is as follows:

Def update <br/> @ user = user. find (Params [: Id]) <br/> respond_to do | format | <br/> Print "User: # {Params [: user]} "<br/> If Params [: User] [" hashed_password "] = Params [: User] [" repassword "] <br/> If @ user. update_attributes (Params [: User]) <br/> FLASH [: Notice] = 'user was successfully updated. '<br/> format.html {redirect_to (@ user)} <br/> format. XML {head: OK} <br/> else <br/> format.html {render: Action => "edit"} <br/> format. XML {render: xml => @ user. errors,: Status = >:unprocessable_entity} <br/> end <br/> else <br/> FLASH [: notice] = 'password and password confirm are not the same' <br/> format.html {render: Action => "edit"} <br/> format. XML {render: xml => @ user. errors,: Status = >:unprocessable_entity} <br/> end

 

5. Modify users/edit.html. ERB and users/show.html. ERB.

Modify the display of the password field

After edit.html. ERB is modified, the Code is as follows:

<H1> editing user </p> </P> <p> <% form_for (@ user) Do | f |%> <br/> <% = f. error_messages %> </P> <p> <br/> <% = f. label: username %> <br/> <% = f. text_field: username %> <br/> </P> <br/> <p> <br/> <% = f. label: Password %> <br/> <% = f. password_field: hashed_password %> <br/> </P> <p> <br/> <% = f. label: password_confirm %> <br/> <% = f. password_field: repassword %> <br/> </P> <br/> <p> <br/> <% = f. submit 'update' %> <br/> </P> <br/> <% end %> </P> <p> <% = link_to 'show ', @ USER %> | <br/> <% = link_to 'back', users_path %>

 

Show.html. ERB:

<P> <br/> <B> Username: </B> <br/> <% = H @ user. username %> <br/> </P> <p> <br/> <B> password: </B> <br/> <% = H @ user. hashed_password %> <br/> </P> <p> <% = link_to 'edit', edit_user_path (@ user) % >|< br/> <% = link_to 'back', users_path %>

 

6. Modify routes. Rb

Add the following ing rules

Map. Connect '/users',: controller => "user",: Action => "Index"

 

Demo effect:

Create user:

 

 

 

Update user:

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.