ทำ Rails Server ให้เป็น Windows Services

Posted by batt | Posted in RubyOnRails, Windows | Posted on 22-11-2011

0

ใช้ตัวนี้ได้เลย http://windowsrailsapp.sourceforge.net/ ใช้ง่าย ติดตั้งง่าย

วิธีติดตั้ง

  1. Download โปรแกรม
  2. ลงโปรแกรมตามปกติ
  3. เปิด service console (start/settings/Control Panel/Admin tools/Services) ดูว่ามี RailsAppLauncher service ถ้ามีเป็นอันเสร็จขั้นตอนการติดตั้ง

Configuration

  1. ไปที่ Folder “C:\Program Files\MA\Rails App Launcher”
  2. แก้ไขไฟล์ RailsAppLauncher.exe.config
  3. แก้ไขตามต้องการ กำหนด path และ port
  4. ตรวจสอบการทำงานโดยเปิด browser ไปที่ http://[servername]:[port]/

gem sources -a

Posted by batt | Posted in RubyOnRails | Posted on 20-02-2011

0

RubyGems will revert to legacy indexes degrading performance.
WARNING:  RubyGems 1.2+ index not found for:
gem sources -a http://gems.github.com
gem sources -a http://gems.rubyforge.org

gem install … แว๊ก ERROR

Posted by batt | Posted in RubyOnRails | Posted on 12-01-2011

0

หลังจากไม่ได้ลง windows ใหม่ปีกว่า…
พอลง ก็ต้องลงโปรแกรมนั่นนี้ และที่สำคัญ Ruby on Rails เครื่องมีทำมาหากิน

ลง ruby ตามปกติผ่านฉลุย ไม่มีอะไรเกิดขึ้น

แต่…. gem install rails(หรืออื่นๆ)

ERROR:  Error installing pg:
The ‘pg’ native gem requires installed build tools.

Please update your PATH to include build tools or download the DevKit
from ‘http://rubyinstaller.org/downloads’ and follow the instructions
at ‘http://github.com/oneclick/rubyinstaller/wiki/Development-Kit’

เหอๆ ยังดีที่บอกว่าให้ทำอะไรบ้าง

ก็ไปโหลด DevKit ตามที่เขาบอกที่ http://rubyinstaller.org/downloads

แล้วทำตามขั้นตอนที่ http://github.com/oneclick/rubyinstaller/wiki/Development-Kit

สรุปให้แบบง่ายๆ unzip DevKit ไว้ที่ไหนก็ได้(แต่ folder ห้ามมีช่องว่าง)
แล้วก็ไปที่ folder นั้น แล้ว run ไฟล์ dk.rb
>> ruby dk.rb init
กับ
>> ruby dk.rb install
เสร็จ

แล้วลอง gem install rails (หรืออื่นๆ) ผ่านฉลุยแน่นอน

Using a proxy with open-uri

Posted by batt | Posted in RubyOnRails | Posted on 08-01-2011

0

ทำโปรเจค EBMWiz  ตอนตั้ง server ไว้ที่ office ก็ใช้งานได้ปกตินี่นา

แต่ทำไมพอย้ายไป server ไปไว้ที่ใน มข. มันใช้ไม่ได้

สงสัยว่าอาจจะต้องใช้ proxy ก่อน

require 'open-uri'
url = 'http://www.pubmed.com/'

proxy_addr = 'http://home.kku.ac.th/proxy.pac:'
proxy_port = '3128'

page = open(url, :proxy => (proxy_addr + proxy_port))

note ไว้ก่อน ลองแล้วได้ผลยังไงจะมา update ให้ฟัง

Remove div.fieldWithErrors from Rails forms

Posted by batt | Posted in RubyOnRails | Posted on 18-12-2010

0

In a decision I have never understood, Rails forms by default add <div> ... </div> around any field in your form that has validation errors on submission. Which sucks when you end up with markup like this:

<p>
  <div><label for="post_title">Title</label></div><br />
  <div><input id="post_title" name="post[title]" size="30" type="text" value="" /></div>
</p>

At work we’ve had a hack in place for a while not that dug into ActionView and turned off this nonsense. We normally don’t go highlighting form fields with errors anyway. As it turns out though, that HTML is actually rendered by a proc you can set. By default it looks like this:

ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }

Just override this proc to return the tag only:

ActionView::Base.field_error_proc = proc {|html, instance| html }

In your environment.rb file, that would be:

config.action_view.field_error_proc = proc {|html, instance| html }

จาก :: http://d.strelau.net/post/163547069/remove-div-fieldwitherrors-from-rails-forms

ตัวอย่างที่ทำเอง เอาไว้ที่ controller

 ActionView::Base.field_error_proc = proc {|html, instance| %{<span>#{html}<span></span></span>} }

ROR Draw image by RMagick

Posted by batt | Posted in RubyOnRails | Posted on 20-11-2010

0

require ‘RMagick’
include Magick


result = Magick::Image.new(1000, 725){ self.background_color = ‘#FFFFFF’ }
watermark = Magick::Image.read(“#{RAILS_ROOT}/public/logo/watermark.png”).first
logo = Magick::Image.read(“#{RAILS_ROOT}/public/logo/logo_xxx.png”).first
signature1 = Magick::Image.read(“#{RAILS_ROOT}/public/logo/sig1.png”).first
signature2 = Magick::Image.read(“#{RAILS_ROOT}/public/logo/sig2.png”).first
student_img = Magick::Image.read(“#{RAILS_ROOT}/public/images/gui/user_90x90.png”).first
result = result.composite(watermark, Magick::CenterGravity, 0, 0, Magick::OverCompositeOp)
result = result.composite(logo, Magick::NorthGravity, 0, 20, Magick::OverCompositeOp)
result = result.composite(signature1, Magick::NorthGravity, -250, 600, Magick::OverCompositeOp)
result = result.composite(signature2, Magick::NorthGravity, 250, 600, Magick::OverCompositeOp)
result = result.composite(student_img, Magick::NorthGravity, 345, 50, Magick::OverCompositeOp)

line1 = Draw.new
result.annotate(line1, 1000,30, 0, 200, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
line1.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line1.gravity = Magick::NorthGravity
line1.pointsize = 18
line1.fill = “#000000″
line1.font_weight = Magick::BoldWeight
}

line2 = Draw.new
result.annotate(line2, 1000,30, 0, 240, “วุฒิบัตรฉบับนี้ให้ไว้เพื่อแสดงว่า”){
line2.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line2.gravity = Magick::NorthGravity
line2.pointsize = 18
line2.fill = “#000000″
line2.font_weight = Magick::BoldWeight
}

student_name = Draw.new
result.annotate(student_name, 1000,30, 0, 280, “#{courses_student.prefix} #{courses_student.firstname}  #{courses_student.lastname}”){
student_name.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
student_name.gravity = Magick::NorthGravity
student_name.pointsize = 18
student_name.fill = “#000000″
student_name.font_weight = Magick::BoldWeight
}

detail = Draw.new
result.annotate(detail, 1000,30, 0, 340, “ได้เข้าฝึกอบรม #{certificate_template.detail}”){
detail.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
detail.gravity = Magick::NorthGravity
detail.pointsize = 18
detail.fill = “#000000″
detail.font_weight = Magick::BoldWeight
}

if courses_student.is_certified
detail2 = Draw.new
result.annotate(detail, 1000,30, 0, 370, “และผ่านเกณฑ์การประเมินผลสัมฤทธิ์การอบรม”){
detail2.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
detail2.gravity = Magick::NorthGravity
detail2.pointsize = 18
detail2.fill = “#000000″
detail2.font_weight = Magick::BoldWeight
}
end

line3 = Draw.new
result.annotate(line3, 1000,30, 0, 4100, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
line3.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line3.gravity = Magick::NorthGravity
line3.pointsize = 18
line3.fill = “#000000″
line3.font_weight = Magick::BoldWeight
}

line4 = Draw.new
result.annotate(line4, 1000,30, 0, 440, “xxxxxxxxxxxxxxxxxxxxx”){
line4.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line4.gravity = Magick::NorthGravity
line4.pointsize = 18
line4.fill = “#000000″
line4.font_weight = Magick::BoldWeight
}

line5 = Draw.new
result.annotate(line5, 1000,30, 0, 470, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx “){
line5.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line5.gravity = Magick::NorthGravity
line5.pointsize = 18
line5.fill = “#000000″
line5.font_weight = Magick::BoldWeight
}

line6 = Draw.new
result.annotate(line6, 1000,30, 0, 500, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
line6.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
line6.gravity = Magick::NorthGravity
line6.pointsize = 18
line6.fill = “#000000″
line6.font_weight = Magick::BoldWeight
}

thai_mon = ["", "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม" "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม"]
date = Draw.new
result.annotate(date, 1000,30, 0, 540, “ให้ไว้ ณ วันที่ #{Date.today.day} เดือน #{thai_mon[Date.today.mon]} พ.ศ. #{Date.today.year + 543}”){
date.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
date.gravity = Magick::NorthGravity
date.pointsize = 18
date.fill = “#000000″
date.font_weight = Magick::BoldWeight
}

sig2_name = Draw.new
result.annotate(line6, 1000,30, 250, 668, “(xxxxxxxxxxxxxxxxxxxxxxxxxxxxx)”){
sig2_name.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
sig2_name.gravity = Magick::NorthGravity
sig2_name.pointsize = 18
sig2_name.fill = “#000000″
sig2_name.font_weight = Magick::BoldWeight
}

sig2_desc = Draw.new
result.annotate(line6, 1000,30, 250, 688, “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”){
sig2_desc.font = “#{RAILS_ROOT}/public/fonts/TH_Niramit_AS_Bold.ttf”
sig2_desc.gravity = Magick::NorthGravity
sig2_desc.pointsize = 18
sig2_desc.fill = “#000000″
sig2_desc.font_weight = Magick::BoldWeight
}

filename = “#{directory}/cert_#{@course.id}_#{courses_student.student_id}_#{courses_student.student_id}.jpg”
result.write(filename)

Ruby Percent Syntax (Percent Functions)

Posted by batt | Posted in RubyOnRails | Posted on 08-09-2009

0

I wanted to post a quick guide to the special ruby syntax for literals that utilize the % (percent) symbol. Most beginners guides to ruby leave out an explanation of these forms of literals, but many ruby coders use them. When someone encounters them for the first time it is almost impossible to figure out what they mean. (Try searching Google for “%w”)

Ruby has special syntax for making strings, arrays and system commands easier to write. They allow you to use different characters as delimiters so you can minimize escaping in your literals.

The syntax

The syntax for the % literals is a percent symbol (%) a letter which defines what kind of literal it is (Q, q, w, x, r) a delimiter,  the content, and the closing delimiter.

The delimiter can be any character, and is defined as whatever is immediately after the letter in the syntax. For example %Q!content! , the delimiter is the ! and it surrounds the content. There are special cases when the delimiter is { or (, the closing delimiter will be } or ) respectively.

%Q and %q (Percent Q): Strings

%Q!Some String of “Characters”! <==> ” Some String of \”Characters\” “

%Q is the equivalent to a double-quoted ruby string. #{expression} evaluation works just like in double-quoted strings, even if you use %Q{} as your delimiter!

You can also leave off the Q and it will have the same functionality. I recommend leaving the Q in to be more clear.

%q!Some String of “Characters”! <==> ‘Some String of Characters’

The %q is just like %Q, but acts the same as a single-quoted string. Whatever is inside the delimiters is returned as a string.

You can remember %Q is for strings because it acts like Quotes.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#string

%W (Percent W): Arrays

%W(North South East West) <==> ["North", "South", "East", "West"]

%W (and %w) allow you to create an Array of strings without using quotes and commas.

The delimiter rules are the same as strings, but typically parentheses are used. The content inside the delimiters are split by white-space, and put into an array. This is great if you have a hard coded list of single word strings.

When using %W (capital W), it is evaluated as a double-quoted string. This allows you to use #{} to interpolate values. %w (lower-case w) will evaluate as a single quoted string.

You can remember %W is by thinking of it as a White-space divided Array.

More info here : http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#array

%x (Percent x): System Execution

%x{ ls /usr/local } <==> `ls /usr/local`

%x allows you to call system commands, equivilent to wrapping the command in `s (grave accents). The benefit of the $x{} syntax is you don’t have to escape your accents in commands that use them.

You can remember to use X because it eXecutes a command.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#command

%r (Percent r): Regular Expressions

%r{/usr/bin/} <==> /\/usr\/bin\//

%r is really handy for regular expressions that contain /s (forward slashes) which are the default delimiter for regular expressions and have to be escaped.

Remember to use %r with regular expressions.

More info here: http://docs.huihoo.com/ruby/ruby-man-1.4/syntax.html#regexp

I hope this information is helpful. Please leave a comment if this helped or if I left something out.

Tags: , , ,

credit : http://jimhoskins.com/2008/10/07/ruby-percent-syntax-percent-functions/

Multiple Database connection on Rails

Posted by batt | Posted in RubyOnRails | Posted on 18-05-2009

0

เกิ่นนำซักเล็กน้อยแล้วกัน…

ทำงานใช้ ROR มาได้ซักพัก ได้ทำ Project GSMIS ของบัณฑิตวิทยาลัย มหาวิทยาลัยขอนแก่น

โอ้ววว…!!!!!!!! เจออะไรที่ต้อง research เยอะมาก

เรื่องแรกเลย…ฐานข้อมูลเราไม่มีข้อมูลที่เก็บไว้เอง ต้อง import

ก่อนหน้านี้ Project นี้ผ่านมือพี่คนอื่นมาก่อนก่อนจะมาถึงเรา

ตอนนั้น…ทุกคนยังใหม่กับ ROR เลยใช้วิธี import จาก Excel ได้ผลดีในระดับ

มีข้อเสียคือเปลืองเวลาคนมานั่ง upload file

และแล้ว GSMIS ก็ได้เริ่มนับหนึ่งใหม่อีกครั้ง ทำให้ได้ทำ research เรื่องแรก คือ

“Multiple Database connection on Rails”

หลังจากเขียนน้ำมาเยอะ มาจริงจักกันได้แล้ว

ถ้าเราจำเป็นต้องใช้ Database มากกว่า 1 ตัว ใน 1 Project

ขั้นแรก…แก้ config ที่ไฟล์ config/database.yml

ก่อนแก้ config/database.yml

 # Default
 defaults: &defaults
   adapter: postgresql
   encoding: unicode
   host: localhost
   database: databasename
   username: username
   password: password
 development:
   <<: *defaults
 test:
   <<: *defaults
 production:
   <<: *defaults

ตามข้างบน ก็จะเป็นปกติที่เราใช้ๆ กัน คือ connect ไปที่ database เดียว ต่อไปจะเป็น code หลังจากแก้ไข

แก้ไขแล้ว config/database.yml

 # Default
 defaults: &defaults
   adapter: postgresql
   encoding: unicode
   host: localhost
   database: databasename
   username: username
   password: password
 development:
   <<: *defaults
 test:
   <<: *defaults
 production:
   <<: *defaults

 # Database ที่ N
 default_Ns: &default_Ns
   adapter: postgresql
   encoding: unicode
   host: localhost
   database: databasename_N
   username: username_N
   password: password_N
 prefixN_development:
   <<: *default_Ns
 prefixN_test:
   <<: *default_Ns
 prefixN_production:
   <<: *default_Ns

พอแก้ไข config/database.yml แล้ว ยังไม่สามารถใช้ได้ ยังต้องไปแก้อีกไฟล์

เอ้ยยย..ไม่สิ เรียกว่า เขียน code เพิ่มดีกว่า

ไฟล์ที่จะไปเขียน code เพิ่มก็คือ … ไฟล์ controller ไหนก็ได้

แต่จะพาเขียนไว้ที่ app/controllers/application.rb (คงรู้นะว่าทำไม ถึงเขียนไว้ที่นี่)

 class ApplicationController < ActionController::Base

  before_filter :set_N_db_connection

  def set_N_db_connection

   # extra_coord = YAML.load(File.open(File.join(RAILS_ROOT, PATH_FILE_CONFIG),"r"))[PREFIX_CONNECTION+ ENV['RAILS_ENV']]
   extra_coord = YAML.load(File.open(File.join(RAILS_ROOT,"config/database.yml"),"r"))[,"prefixN_"+ ENV['RAILS_ENV']]

   # MODEL_CLASS_NAME.establish_connection(extra_coord)
   TableName.establish_connection(extra_coord)

  end

 end

TableName คืออะไร มันก็คือ class name ของ model นั่นเอง

เพราะฉะนั้น เราก็ต้องมี model TableName (app/model/table_name.rb) ด้วย

เท่านี้ก็เรียบร้อย