Bài đăng

Hiển thị các bài đăng có nhãn Technical stuff

Regular Expression in Ruby

In this post, I intend to write down some basic Regex knowledge, especially for Ruby. Syntax A regular expression literal is a pattern between slashes or between arbitrary delimiters followed by %r as follows: /pattern/ /pattern/im # option can be specified %r!/usr/local! # general delimited regular expression Basic patterns Except for control characters, (+ ? . * ^ $ ( ) [ ] { } | \ ), all characters match themselves. You can escape a control character by preceding it with a backslash. Following table lists the regular expression syntax that is available in Ruby. Patterns Description ^ Matches beginning of line. $ Matches end of line. . Matches any single character except newline. Using m option to allow it to match newline as well. […] Matches any single character in brackets. [^…] Matches any single character not in brackets re* Matches 0 or more occurrences of preceding expression. re+ Matches 1 or more occurrence of preceding expression. re? ...

Beautiful controller in Ruby on Rails

Your controllers is too fat and messy. You want to make them better. You should be follow these rules: Short, DRY, easy to read Minimum amount of glue code between request and model Unless there is good reason, controller should be follow a standard When user’s interactions are normalized to CRUD. Our controller could be like this class ObjectController < ApplicationController def index load_objects end def show load_object end def new build_object end def create build_object save or render 'new' end def edit load_object build_object end def update load_object build_object save or render 'edit' end def destroy load_object @object . destroy redirect_to objects_path end private def object_params obj_params = params [ :object ] obj_params ? obj_params . permit ( :id , :attributes ) : {} end def loa...

3 common security issues in Rails

SQL injection Đây là kiểu tấn công gây ảnh hưởng lên câu truy vấn dữ liệu thông qua các tham số của ứng dụng web. Thường thì chúng được dùng để vượt qua bước chứng thực hay để đánh cắp dữ liệu. Ví dụ bạn muốn tìm những project có tên được lấy từ tham số name: Project.where("name = '#{params[:name]}'") Tham số này có thể là  'OR 1 -- . Kết quả câu truy vấn là: SELECT * FROM projects WHERE name = '' OR 1 --' (trả về là tất cả projects). Hay nếu tham số là ') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users -- thì câu truy vấn dữ liệu là: SELECT * FROM projects WHERE (name = '') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users --'  (lấy name và password của tất cả users). Cách phòng chống: Ruby on Rails đã xây dựng 1 filter để lọc các ký tự đặc biệt trong SQL:  ' ,  " ,  null  và xuống dòng. Filter này được dùng tự động cho câu lệnh  find  hay  find_by_something . V...

Two common CSS problems I usually meet and solution

Hình ảnh
Footer need to be always at the bottom Most of web applications have a footer. Footer is for your company or organization’s infomation, copyright, etc. And as its name, it need to be at the bottom. But somethime your body content is too short, your footer is still at the bottom of the page, but not the bottom of viewport. Like this Solution: HTML <div id= "container" > <div id= "header" ></div> <div id= "body" ></div> <div id= "footer" ></div> </div> CSS html , body { margin : 0 ; padding : 0 ; height : 100% ; } #container { min-height : 100% ; position : relative ; } #body { padding-bottom : 60px ; /* Height of the footer */ } #footer { position : absolute ; bottom : 0 ; width : 100% ; height : 60px ; /* Height of the footer */ } References:  http://matthewjamestaylor.c...

Open class in Ruby

Trong ruby, không có sự khác biệt thực sự giữa code định nghĩa lớp và code của bất cứ loại nào khác. Nếu một lớp đã tồn lại, Ruby không định nghĩa nó lần nữa mà chỉ mở lớp này ra thêm các các methods mới vào. Có vẻ trong Ruby, chữ  class  giống một scope operator hơn là để khai báo lớp. Ví dụ: class OneClass def x "x" end end class OneClass def y "y" end end obj = OneClass . new obj . x # => "x" obj . y # => "y" Ở đây chúng ta có thể dùng kỹ thuật này (Open Class) để mở các lớp có sẵn, thậm chí là các lớp của Ruby, để chỉnh sửa. Kỹ thuật này giúp chúng ta có thể thêm thắt, viết lại cái hàm của lớp đã được định nghĩa trong thư viện (monkey patching). Mặt trái của nó là khi chúng ta chỉnh sửa hành vi các phương thức của thư viện chuẩn sẽ làm phần mềm khó test và kiểm soát. Nhiều khi khiến phần mềm chạy lỗi hoàn toàn. Có một số người khó tính thưòng hoàn toàn không khuyến khích các lập trình viên s...

Ruby on Rails Directory Structure

Hình ảnh
Setup Ruby on Rails in Ubuntu You can setup Ruby on Rails in Ubuntu by following the inductions in  https://gorails.com/setup/ubuntu/14.10  .  Ruby on Rails Directory Structure After setting up Ruby and Rails, you can new a Rails app with command:  rails new your_app_name . So you can see that rails generates automatically a bunch of directories and files. I will show you what they use for. |-- app | |-- assets | | |-- images | | |-- javascripts | | | `-- application.js | | `-- stylesheets | | `-- application.css | |-- controllers | | |-- application_controller.rb | | `-- concerns | |-- helpers | | `-- application_helper.rb | |-- mailers | |-- models | | `-- concerns | `-- views | `-- layouts | `-- application.html.erb |-- bin | |-- bundle | |-- rails | |-- rake | |-- setup | `-- spring |-- config | |-- application.rb | |-- boot.rb | |-- database.yml | |-- environment.rb ...

Vim basics

Vim editor Vim editor is a powerful editor, specialy working with server. You may love it. How to install it in Ubuntu Run these command in terminal sudo apt-get update  to make sure you have the updated sources.list sudo apt-get install vim  to install vim. Open a file and do simple with it Open a file and type something. vim path-to-file  to open a file on  path-to-file  with vim. In a standard editor, typing on the keyboard is enough to write something and see it on the screen. Not this time. Vim is in Normal mode. Let’s go to Insert mode. Type the letter  i . You should feel a bit better. You can type letters like in a standard editor. To get back to Normal mode just press the  ESC  key. Some commands in Normal mode of Vim You now know how to switch between Insert and Normal mode. And now, here are the commands that you need in order to survive in Normal mode: i  → Insert mode. Type  ESC  to return t...

Git and Github basics