Chen Log

陳洛格

Posts Tagged ‘ruby

AutoLink on ruby on rails

leave a comment »

Because the AutoLink on rails has removed in rails 3.1,

so if you have to use the function you must be install other gem: 「Rinku

 

Before you start to install the Rinku Gem,

you must install the Xcode app and agree the license.

then 

$ gem install rinku

 

  • Inserted require 'rinku' into the controller file
  • Inserted the following into the view file <% text = "Go to http://www.abc.com or email me at dude@abc.com" %> and <%= Rinku.auto_link(text, mode=:all, link_attr=nil, skip_tags=nil) %>

key those text on the view file:

auto_link('http://www.pokemon.com', :all, 'target="_blank"')

Reference this url:
https://github.com/vmg/rinku 
http://stackoverflow.com/questions/6418710/replacing-the-auto-link-method-in-ruby-on-rails-3-1

Advertisements

Written by chenagw

2014/04/21 at 00:40

Posted in Uncategorized

Tagged with , , ,

Rails ruby資料形態

leave a comment »

Here are all the Rails3 (ActiveRecord migration) datatypes:

:binary
:boolean
:date
:datetime
:decimal
:float
:integer
:primary_key
:references
:string
:text
:time
:timestamp

Source:http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column

Written by chenagw

2014/02/25 at 13:30

Posted in Uncategorized

Tagged with , ,

去掉 Float 浮點數字 後面的 0

leave a comment »

如何把 55.0 變成 55?
這是ruby浮點運算常需要遇到的問題。
 

float.to_s.gsub(/(\.?0+$)/,””).gsub(/(\.+)/,”.”)

直接把浮動數字後面多餘的零去掉!

參考網頁:

http://railsforum.com/viewtopic.php?id=24996

Written by chenagw

2013/01/19 at 00:47

Posted in Uncategorized

Tagged with ,

Ruby on Rails 字串連接方法

leave a comment »

PHP的連接是用點 “ . ”  如這樣:

$a=”500″;
$b=”apple”;

echo $a.$b;

// print: ” 500apple” 

而Java的話是採用 “+” 加號。

Ruby String字串與Value變數的的連接長的比較變態一點,
是這樣的:

$str = "#{@str1}ABCD#{@str2}CFDE"

 

沒錯!
就是引號內加上#{@變數} = “#{@變數} “

Written by chenagw

2013/01/11 at 13:46