Rails

การทำ highlight ให้ text ของ Rails

งานที่ทำในตอนนี้ต้องทำในส่วนของการค้นหา โดยต้องมี highlight ให้ text ที่ตรงกับคำค้นหา ซึ่งการให้ทำ highlight นั้น Rails Framework ได้เตรียม text_helper ไว้ให้ใช้งานแล้ว โดยมีลักษณะดังนี้

highlight(text, phrase, highlighter = '<font color="#ff0000">\1</font>')

ตัวอย่าง คือ <%= highlight('You searched for: rails', 'rails') %>
ผลลัพธ์ที่ได้ คือ You searched for: rails

แต่ข้อด้อย คือ ไม่รองรับการทำ highlight ของคำหลายๆ คำ เช่น

2
Your rating: None Average: 2 (1 vote)

การใช้ calendar_helper ใน Rails

ในบางครั้งเราจำเป็นต้องมีการใช้ปฏิทินเพื่อแสดงเหตุการณ์ที่เกิดขึ้นของแต่ละวัน หรือแสดงวันที่มีการประชุม เป็นต้น โดยมากนั้นเราจะใช้ JavaScript ในการทำปฏิทิน แต่สำหรับคนที่เล่น RoR นั้น จะมี plugin ที่ชื่อว่า calendar_helper มาให้ใช้ เรามาดูวิธีการใช้งานกันดีกว่า

1. ติดตั้ง plugin โดยใช้คำสั่งดังนี้

ruby script/plugin install calendar_helper

2. ให้ generate calendar เพื่อให้สามารถใช้ css ที่มีมาให้ได้ (มี 3 สีมาให้เลือก คือ แดง น้ำเงิน เทา) โดยใช้คำสั่งดังนี้

ruby script/generate calendar

0
Your rating: None

การตั้งค่าให้ Rails ใช้งาน Gmail SMTP Server

1. ให้นำ code ด้านล่างนี้ไปไว้ใน directory ชื่อ lib และตั้งชื่อไฟล์คือ smtp_tls.rb

require "openssl"
require "net/smtp"
 
Net::SMTP.class_eval do
  private
  def do_start(helodomain, user, secret, authtype)
    raise IOError, 'SMTP session already started' if @started
    check_auth_args user, secret, authtype if user or secret
 
    sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
    @socket = Net::InternetMessageIO.new(sock)
    @socket.read_timeout = 60 #@read_timeout
 
    check_response(critical { recv_response() })
    do_helo(helodomain)

0
Your rating: None

การใช้ autocomplete ใน Rails

สร้างฐานข้อมูลโดยมี table ชื่อ dishes และมี field ดังนี้

create table dishes (
  id int not null auto_increment,
  name varchar(50) not null,
  descript varchar(100),
  primary key(id)
);

ใส่ข้อมูลลงในฐานข้อมูลที่ได้สร้างขึ้นมา โดยข้อมูลตัวอย่างมีดังนี้

insert into dishes (name, descript) values ("ข้าวผัดกะเพราหมูแดง", "ผัดกะเพราหมูแดง");
insert into dishes (name, descript) values ("ข้าวคะน้าหมูกรอบ", "ผัดคะน้าใส่หมูกรอบ");
insert into dishes (name, descript) values ("ข้าวคอหมูย่าง", "เนื้อหมูย่างราดน้ำจิ้มเผ็ด");

0
Your rating: None

การใช้ภาษาไทยร่วมกับ RJS

เพิ่ม code ด้านล่างนี้ไว้ใน controllers/application.rb

after_filter :set_charset
 
def set_charset
  content_type = @headers["Content-Type"] || 'text/html'
  if /^text\//.match(content_type)
    @headers["Content-Type"] = "#{content_type}; charset=utf-8"
  end
end

0
Your rating: None

Building RMagick on Mac OS X

ติดตั้ง freetype

curl -O http://download.savannah.gnu.org/releases/freetype/freetype-2.1.10.tar.gz
tar -xzvf freetype-2.1.10.tar.gz
cd freetype-2.1.10
./configure --prefix=/usr/local
make
sudo make install
cd ..

ติดตั้ง libpng

curl -O http://superb-west.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.10.tar.bz2
bzip2 -dc libpng-1.2.10.tar.bz2 | tar -xv
cd libpng-1.2.10
./configure –-prefix=/usr/local
make
sudo make install
cd ..

ติดตั้ง jpegsrc

 

0
Your rating: None