Search


Meta

Creative


Support


Leesoft

Feed™

Recent Comments

Photos

Apr
24

用Ruby的Win32 Utils访问剪切板

Posted by » developerly
51 hits

WIN32-CLIPBOARD这个库提供了对Windows剪切板的便利访问方法

 # Example:
 require “Win32API”  #这一步很容易被忽略 
 require “win32/clipboard”
 include Win32

 # Get the data on the clipboard
 puts “Data on clipboard was: ” + Clipboard.data

 # Set the data on the clipboard
 Clipboard.data = “test”

 # Printing the data again, we get “test”
 puts “Data on clipboard is now: ” + Clipboard.data

 # Clear the clipboard
 Clipboard.empty

你可以在http://rubyforge.org/projects/win32utils下载这个包

Apr
23

菜鸟财经:说说今天的大盘和中石油

Posted by » developerly
48 hits

股市从来都是充满了不确定性和直觉。我就是一个标准的菜鸟,这是我在网上的一亩三分地,所以不妨说两句。现在是中午10点,我预计下午开始大盘要强力反弹,中石油的股票价格会上升!明天很可能会出现更多的政策利好消息~~~如果说现在的股市是外资和国家争夺的战场,那么短期内会有一次国家战术上的胜利,至于未来战争的输赢,貌似目前还无可知。

Apr
15

Ruby获取当前的执行文件的路径和目录

Posted by » developerly
208 hits

获得当前执行文件的文件名:

__FILE__

获得当前文件的目录

File.dirname(__FILE__)

获得当前执行文件的完整路径

require ‘pathname’
Pathname.new(__FILE__).realpath

获得当前执行文件的目录完整路径

require ‘pathname’
Pathname.new(File.dirname(__FILE__)).realpath

如果使用$0: $0是整个ruby执行文件最顶层文件的路径。因此使用if __FILE__==$0可以判断当前ruby文件是被引用还是被执行。

Apr
15

Rails文档阅读之Render

Posted by » developerly
72 hits

很多时候我们疑惑,疑惑的原因仅仅是因为我们懒的去看文档。最近在用RoR开发一个简单的系统。很多帮助其实就来自于官方文档。下面的内容来自官方文档,对Rails中的Render方法做了一个全面的总结。最近比较忙,只能等有时间了再好好做个翻译。
跳转后阅读Rails文档阅读之Render的完整内容

Apr
10

转载:Restful Rails风格中自定义action

Posted by » developerly
51 hits

忘了从哪里转过来的了,欢迎原作者和我联系{@AT=”@”;”stephen.liy#{@AT}gmail.com”}

自定义action,对于不能归结为crud操作的action,我们需要自己定义action,已经说过,REST把所有的远程调用抽象为对远程资源的 CRUD操作,非CRUD操作应当转化或者说抽象成CRUD操作,比如对于project可以有一个关闭操作close,我们可以把它理解成一个http POST请求去修改project的closed字段为true,这样一来这个操作也可以当作CRUD操作了。需要做的是在routes.rb增加一行:

map.resources :projects, :member => { :close => :post }

定义close action是POST方法,在Controller增加close方法:

def close
respond_to
do |format|
if Project.find(params[:id]).update_attribute(:closed, true)
flash[
:notice] = Project was successfully closed.
format.html { redirect_to projects_path }
format.xml { head :ok }
else
flash[
:notice] = Error while closing project.
format.html { redirect_to projects_path }
format.xml { head 500 }
end
end
end

你可以通过http://localhost:3000/project/:project_id;close来调用此方法,请注意,POST的方法需要通过Form来实现,因此我们使用button_to:

<td><%= button_to Close, close_project_path(project) %></td>

自定义action不仅仅可以使用REST风格,传统的controller/action/id的方式仍然可以使用,注意下routes.rb的最后两行即可。

Apr
10

ActiveView中的content_for_*,content_for(*),yield

Posted by » developerly
61 hits

使用content_for方法可以把你的erb文件分割成几个部分。从而嵌入到布局模板不同的位置中去。考虑下面一个典型的应用场景

<%#layout.html.erb%>
<html>
<head><title><%=@content_for_title%></title>
</head>
<body>
<%=@content_for_welcome_message%>
<%=@content_for_layout%>
</body>
</html>

在我们的相应controller/action下面的erb可以写成这样

<%#index.html.erb  -%>
<% content_for(”title”) do%>
Welcome to leesoft
<% end%>
<% content_for(”welcome_message”) do%>
<h1>HOME</h1>
<% end%>
bla,bla,bla. Today is my birthday!

最终得到的HTML页面为

<html>
<head><title>Welcome to leesoft
</title>
</head>
<body>
<h1>HOME</h1> bla,bla,bla. Today is my birthday! </body>
</html>

其中Content_for(name)是一个包装器,用于捕获存储在一个实例变量内的段,并赋值给@content_for_<name>变量。
更好的做法。

在Rails2.0下面,我们还有更加优雅的解决方法。
跳转后阅读ActiveView中的content_for_*,content_for(*),yield的完整内容

Apr
3

before_filter使用时所需要注意的问题

Posted by » developerly
76 hits

在使用Rails2.0的过程中还真的有很多需要注意的问题。这里有一个,我在网上查了很多次,都没有找到满意的答案,最终还是我自己解决了。在对controller使用前置过滤器的时候,我们可能会写如下的代码:

class MainController < ApplicationController  
  before_filter do |control|

    redirect_to :control=>user,:action=>login \
              unless session[:username]

  end

  def index

  end

end

运行的时候报错

Symbol as array index

如果代码修改为:

class MainController < ApplicationController

  before_filter :auth

  def index

  end

  private:

  def auth

    redirect_to :control=>user,:action=>login \
       unless session[:username]

  end

end

则可以正确运行。经过调查,发现session其实是Controller对象中的一个属性,所以不妨可以使用这样的方法来做(使用control.session[…]):

class MainController < ApplicationController  
 before_filter do |control|

   redirect_to :control=>user,:action=>login \
                 unless control.session[:username]

 end

 def index

 end

end

至此,问题得到了解决。

Apr
2

Everything Ruby:Migration

Posted by » developerly
63 hits

在网站开发中,怎样更好的衔接数据库设计和网站代码开发本身就是一个很有难度的问题。另一方面,怎样更好的对数据库sheme定义以及数据库的移植做好管理也是一个大课题。Rails提供了Migration功能,为此提供了一个解决思路。

Migration位于ActiveRecord名空间下。按照官方API的解释,
Migrations can manage the evolution of a schema used by several physical databases. It‘s a solution to the common problem of adding a field to make a new feature work in your local database, but being unsure of how to push that change to other developers and to the production server. With migrations, you can describe the transformations in self-contained classes that can be checked into version control systems and executed against another database that might be one, two, or five versions behind.

简单翻译一下: Migration能够对位于不同的数据库定义进行管理。主要用来解决开发过程经常遇到的一个问题:当你更新了本地数据库定义后,怎么能让其他开发人员甚至生产服务器上的数据库同步得到更新。通过使用migration,你能够使用自包含的类来描述这个变化,被版本控制工具进行管理,并在其他服务器上执行指定的数据库定义版本。
跳转后阅读Everything Ruby:Migration的完整内容

Apr
1

解决no such file to load — sqlite3

Posted by » developerly
599 hits

这是一个浅的不能再浅的问题了,用rails命令来建立一个web app

> rails myapp

在rails2.0中,生成的代码默认使用sqlite作为默认数据库。刚好我又没有安装sqlite的ruby驱动,所以运行的时候会报错

“no such file to load”

其实我是要使用mysql数据库的,于是使用这个命令:

> rails -d mysql myapp

问题得到了解决。