2010年08月15日 星期日
假设我们有Post和Comment两个Model,我想在Post的show页面里做一个链接来删除一个Comment。
首先让我们看一下Post, Comment以及routes的代码。
# post model
has_many :comments
# comment model
belongs_to :post
# routes.rb
resources :posts do
resources :comments
end
在Post的show.html.erb文件里创建一个链接:
<% @article.comments.each do |comment|
<%= comment.content %>
<%= link_to "Delete", article_comment_path(@article, :comment_id => comment), :method => :delete %>
<% end %>
This will call the destroy action of comments_controller, so we need to create that method.
# in comments_controller.rb
def destroy
comment = Comment.find(params[:comment_id]
comment.destroy
redirect_to request.referer
end
:comment_id就是我们在view里设置的参数名。
Tags: rails
Posted in Ruby | No Comments »
2010年08月05日 星期四
在百度搜索了下自己网站的关键字,发现根本没有被收录。。。
也难怪几乎没写过什么中文的东西嘛。
所以搜索了下如何让自己的网站被百度收录,
地址为:百度-网站登录
有如要求:
· 一个免费登录网站只需提交一页(首页),百度搜索引擎会自动收录网页。
· 符合相关标准您提交的网址,会在1个月内按百度搜索引擎收录标准被处理。
· 百度不保证一定能收录您提交的网站。
这里有详细的指南: 百度-网页搜索帮助-站长FAQ
Tags: baidu, SEO
Posted in SEO | No Comments »
2010年07月27日 星期二
我们将创建一个Helper方法来输出”*”表示必须项目。
(原谅我这蹩脚的汉语,实在是不知道这些术语用中文该怎么叫)
首先在application_helper里创建一个叫mark_required的方法。他的第一个参数为对象,第二个参数接收的是对象的属性。
# application_helper.rb
def mark_required(object, attribute)
"*" if object.class.validators_on(attribute).map(&:class).include? ActiveModel::Validations::PresenceValidator
end
然后在View里我们只要把@user和:name传过去即可。如果你的User Model对:name进行了必须验证(presence validator),”*”就会显示出来。
<div class="field">
<%= f.label :name %><%= mark_required(@user, :name) %><br />
<%= f.text_field :name %>
</div>
链接:http://railscasts.com/episodes/211-validations-in-rails-3
Tags: rails, Ruby
Posted in Ruby | No Comments »
2010年07月25日 星期日
从这里学到的东西:http://railscasts.com/episodes/211-validations-in-rails-3,算是作个笔记吧。
我用的是Rails 3.0 beta4。
假设你有一个User Model,要对期email属性尽兴验证。
class User < ActiveRecord::Base
validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i }
end
这里用:format =>的一窜代码显得很不美观,我们就把这段代码抽出来单独做成一个方法。
在lib文件夹下创建email_format_validator.rb的文件。
# lib/email_format_validator.rb
class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
object.errors[attribute] << (options[:message] || "is invalid")
end
end
end
然后修改User.rb,使用我们新建的方法:email_format
#User.rb
class User < ActiveRecord::Base
validates :email, :email_format => true
end
注意这里的:email_format会自动去寻找EmailFormatValidator这个class。
Tags: rails
Posted in Ruby | No Comments »
2010年07月20日 星期二
在使用Rails的has_and_belongs_to_many的时候,遇到了ActiveRecord::HasAndBelongsToManyAssociationWithPrimaryKeyError异常。内容如下:
Primary key is not allowed in a has_and_belongs_to_many join table (articles_users).
也就是说articles_users的表里设置了PK所导致。
将:id => false添加到migration的create_table语句后面
create_table :articles_users, :id => false do |t|
t.integer :article_id
t.integer :user_id
end
Tags: Ruby
Posted in Ruby | No Comments »
2010年07月19日 星期一
sqlite DB_NAME
DB_NAME即是数据库名又是文件名。因为SQLite的一个文件就是一个数据库。比如Ruby开发中常用的:
sqlite db/development.sqlite3
.tables
.ta
.schema TABLE_NAME
.help
.quit
.exit
更多请参照命令Command Line Shell For SQLite。
Tags: sqlite
Posted in sqlite | No Comments »
2010年07月07日 星期三
一般由点”.”开头的都是隐藏文件或文件夹,这些在Finder里面是看不到的,必须按以下方式才能使Finder显示隐藏文件。
- 打开Terminal(Applications/Utilities)
- 输入下面的命令,按return执行
defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder
- killall Finder就会使命令立即生效,此外强制结束Finder进程,或者登出都可以使命令生效
和上面的步骤完全一样,只是敲的命令不一样。
defaults write com.apple.finder AppleShowAllFiles -bool false
Tags: Mac
Posted in Mac | No Comments »
2010年07月03日 星期六
有一次因为iTunes里的程序过旧而且好多没用的我都直接在iPhone上删除掉了,找出哪些程序被删掉了然后再在iTunes里一一删除太麻烦,我就把iTunes里的程序一口气都删了。结果下次想同步iPhone的时候提示消息告诉我要删除掉iPhone里的所有程序。。。真是开玩笑。。。
在iTunes里右键电击你的iPhone,从菜单里选择“Transfer Purchase”,就是传输购买过的程序的意思。
这样当你主用iPhone下载程序,与iTunes里出现较大差别的时候就可以直接干掉iTunes里的东西,然后用上面这个方法迅速整理好。
Tags: iPhone
Posted in iPhone | No Comments »
2010年06月24日 星期四
安装iOS4以后,在你的邮件设置里会多出“备忘录”和“日历”的选项。
设置为ON以后memo和日历就会和gmail,google calendar进行同步。




Tags: Gmail, Google, iOS4, iPhone
Posted in iPhone | 1 Comment »
2010年06月23日 星期三
Tags: WordPress
Posted in WordPress | No Comments »