About methods share between MVC   
  
2011-07-27


1. call controller method in view.
helper_method is useful when the functionality is something that's used between both the controller and the view. A good example is current_user.
in controller:
helper_method: some_method_can_be_used_in_view


you can also use controller method in view without define by:
@controller.controller_method



2.call helper method in controller.
extend the helper to an object, and call it:
app_helper = Object.new.extend(ApplicationHelper)
app_helper.test_server?



3. call helper method in model.
pretty easy:
helper.helper_method



100. helper :all
is also used in controller, but do not have much relation with method share. it just invokes require on all helper classes within the app/helpers directory.


tags: rails, helper
  


Comments (3)







我回来了   
  
2011-04-10


上一篇日记的时间还定格在去年的 11 月份,转眼间4个多月过去了。倒不是因为生活安逸,我想最大的原因还是我变得懒散了,懒得去记录了,嫌记下这些文字麻烦了。
今天我又开始骑自行车上下班了。公司离家并不远,但因为路上过多的车和几百米一个的绿灯,还是要40分钟左右。
骑在路上,看着两旁渐渐变绿的树叶,享受春风吹在脸上的感觉。
回想起之前记录的几件应该做的事情,加上今天的感受,整理出下面以后坚持要做到的几点:

  • 看设计模式
  • 每天锻炼
  • 早睡早起
  • 早上床15分钟看书
  • 运用潜意识
  • 工作间歇时运动
  • 看经典电影
  • 吃多核桃
  • 骑自行车上下班
  • 记日记




tags: life
  


Comments (0)







Fill PDF form with Ruby    
  
2010-11-23


There are lot of tools can manage PDF in Ruby. Like PDF::Writer, Prawn or PDFKit. But seems has no one can fill form in an exists PDF.

Google it for a while, seems only Java and C# have this kind of tool: iText(#)

Then we can do this in Ruby by calling Java lib. Two solutions: RJB or jruby.
Cause our application is totally writen in Ruby, I choose Rjb.
Also I found this very cool gem: pdf-stamper to make calling java methods easier. Let's go!


Install RJB
(here is a tutorial)

I already has java on my mac, so just try to install RJB by
sudo gem install rjb


Then got error:
checking for jni.h... no
extconf.rb:67:in `create_rjb_makefile': no jni.h in -I$(topdir) -I$(hdrdir) -I$(srcdir) -I"/System/Library/Java
/JavaVirtualMachines/1.6.0.jdk/Contents/Home/include" -I"/System/Library/Frameworks/JavaVM.
framework/Headers" (RuntimeError) from extconf.rb:77


Google, got this valuable infomation:
As of the release of Java for Mac OS X 10.6 Update 3, the Java runtime ported by Apple and that ships with
 Mac OS X is deprecated. Developers should not rely on the Apple-supplied Java runtime being present 
in future versions of Mac OS X.


Posted 2010.10.20, about a month ago, how lucky (╬ ̄皿 ̄)凸

Download java from here, install and set JAVA_HOME in .bash_profile
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework"

Then RJB installed well.


Install pdf-stamper
pdf-stamper is a cool gem makes calling Java method much easier.
You can Install it from gem:
sudo gem install pdf-stamper

Or from github:
https://github.com/jaywhy/pdf-stamper/

Or from my Fork:
https://github.com/zhangzhe/pdf-stamper.

I add a show method to display all keys from PDF template and a method to fill radio_button.


The last things are symple.
Here is pdf model of mine:
class Pdf < ActiveRecord::Base  
  require 'pdf/stamper/rjb'
  require 'pdf/stamper'
  def self.show
    pdf = PDF::Stamper.new("public/form_test.pdf")
    pdf.show
  end

  def self.new(params)
    pdf = PDF::Stamper.new("public/form_test.pdf")
    pdf.checkbox "agree" if params[:agree]
    pdf.radio_button "confirm" if params[:confirm]
    pdf.text "date", params[:date]
    pdf.text "sign", params[:name]
    pdf.save_as("public/done.pdf")
  end
end

show will describe keys in the template;
new will fill pdf form with input.

By the way, creating a PDF form need this very cool solfware: Adobe Acrobat Pro.

Now go ( ̄︶ ̄)↗ 




tags: pdf, ruby, rails
  


Comments (2)







如何制止 垃圾评论   
  
2010-10-19


前一阵子博客被 垃圾评论 搞得很烦,最多时一天甚至会超过100条 。。。

因为之前看过关于使用 Akismet 来防止 垃圾评论 的方法,就直接找到 Rails 的一个相应 插件.

Akismet使用起来很简单,结合 Google Analytics 使用的话可以获取更多的用户信息。 除了常见的垃圾评论外,Akismet 还具有自学习功能。如果发现了它不能识别出的 垃圾评论,你可以告诉它;一旦他把正常回复识别成 垃圾评论,也可以纠正它,让它会根据当前信息进行学习
Ryan Bates 还为 Akismet 做了一段专门的视频讲解

事实上使用初期我发现效果不是很明显。一部分 垃圾评论 被识别出来,但还是有相当一部分被了出去。于是又加入了另一个简单的方法: leave me along

这可能是最简单但十分有效的一个方法。垃圾评论程序 是遍历所有的输入框,并输入随机信息,同时把他想注入的信息附加在其中(如网址等)。 因此 leave me along 方法的原理只是做一个让用户不要输入内容的输入框。在提交时一旦这个输入框有内容,即识别当前评论者为 垃圾评论程序,并提交给 Akismet 记录学习。

实践证明这种方法结合起来非常好用。一周内没有发现任何 垃圾评论 注入。

当然我还是比较懒了,其实完全可以把 leave me along 输入框做的很小,甚至用 css 使其在页面上看不到。这样用户无法看到,也就不知道他的存在;而 垃圾评论 程序还是会笨笨的在里面添加信息 ╮(╯▽╰)╭


tags: spam
  


Comments (8)







螃蟹   
  
2010-10-14


昨天得知水木在团购雪蟹,立刻就预定了。
下班后跑去城北市场,已经没有雌蟹了。挑了两只雄蟹,3斤8两。迫不及待回家,路上还打电话给老妈显摆了一下:)


关于螃蟹,我最早的记忆是儿时的画册。海底世界,各种鱼类贝类,几乎都没有见过,只是照着画册画。螃蟹给我的印象是坚硬和神秘
第一次见到螃蟹是一个小朋友带来的螃蟹爪,拽住里面的筋,就可以让爪子一开一合了。
第一次吃螃蟹则是来到秦皇岛后,几只又小又的螃蟹。当时还想,这么麻烦,这么点肉,怎么会有人爱吃阿?
而真正喜欢吃海鲜还是来到了北京之后。怀念家乡的海风,海边的咸味,还有海鲜的腥味。于是一发不可收拾。皮皮虾成了是我的最爱。螃蟹由于较贵,被我列为奢侈品,只是偶尔馋的不行会吃一次。
以往吃得最多的还是花盖蟹梭子蟹,偶尔会吃膏蟹河蟹,也吃过几次面包蟹帝王蟹。这次的是日本海空运来的雪蟹,自然不能放过他们了 ~

扯远了,上图


合影


洗干净下锅


红了


加工处理后装盘

[/img]
侧身特写

觉得最好吃的部分还是蟹腿,壳里满是肉。味道很鲜,而且可能是深海蟹的缘故,煮出来有些。真的很寒,我的铁肚子都有点受不了。建议吃的时候吃些姜,喝点白酒


附雪蟹说明:
雪蟹又名松叶蟹、津和井蟹,是生存繁衍在日本海、俄罗斯海域200~600米深海的大型蟹类,名列日本海“三大蟹王”
之一,身体颜色为暗红色,背甲呈三角形,有些雄性雪蟹张开蟹足后可达70厘米左右。雪蟹是杂食偏肉性蟹类,
习性生猛,喜食贝类、多毛类等底栖无脊椎生物。


tags: life
  


Comments (7)