- 浏览: 94257 次
- 性别:
- 来自: 成都
最新评论
-
xylffxyfpp:
应该改成慎用each
万恶的"delete",慎用数组的delete -
xylffxyfpp:
a = [1,2,3,4,5,6]
a.each do ...
万恶的"delete",慎用数组的delete -
CaiDeHen:
受教了,我也不知道当时为什么会在这个地方这样子用。其他地方也是 ...
万恶的"delete",慎用数组的delete -
wosmvp:
感觉太乱了为什么不在User模型中来个 named_scope ...
万恶的"delete",慎用数组的delete -
jiachengxi38:
不知道楼主这样的逻辑想法从何而来,权限访问的思想好像在lZ这里 ...
万恶的"delete",慎用数组的delete
文章列表
If you need to create an advanced search with a lot of fields, it may not be ideal to use a GET request as I showed in episode 37. In this episode I will show you how to handle this by creating a Search resource.
<!-- views/searches/new.html.erb -->
<% form_for @search do |f| %>
<p ...
- 2008-12-12 13:27
- 浏览 742
- 评论(0)
In Rails 2.1 we now have the ability to set gem dependencies. Now it's easier than ever to specify which ruby gems our rails app relies on.
# config/environment.rb
config.gem "RedCloth", :version => ">= 3.0.4", :source => "http://code.whytheluckystiff.net/"
co ...
- 2008-12-12 12:57
- 浏览 760
- 评论(0)
Rails 2.1 keeps track of the changes you make to a model's attributes. It also allows you to see what the previous value was. But watch out for the gotcha! See this episode for details.
# config/initializers/active_record_settings.rb
ActiveRecord::Base.partial_updates = true
# change tracking met ...
- 2008-12-12 12:54
- 浏览 798
- 评论(0)
The named_scope method in Rails 2.1 makes performing finds on models very elegant and convenient. See how in this episode.
# models/product.rb
class Product < ActiveRecord::Base
belongs_to :category
named_scope :cheap, :conditions => { :price => 0..5 }
named_scope :recent, lambda { ...
- 2008-12-12 12:52
- 浏览 748
- 评论(0)
Migrations now have a timestamp as their version number in Rails 2.1. In this episode I will explain this change as well as show you some other cool additions.
# in a migration file
def self.up
change_table :products do |t|
t.rename :released_on, :released_at
t.change :description, :tex ...
- 2008-12-12 12:49
- 浏览 690
- 评论(0)
In the past, time zones have been very difficult to work with, but no longer! Rails 2.1 brings with it great time zone support as you can see in this episode.
rake time:zones:local
# config/environment.rb
config.time_zone = "Pacific Time (US & Canada)"
# controllers/application. ...
- 2008-12-12 12:48
- 浏览 718
- 评论(0)
If you're running a production site, it's a good idea to record all exceptions which occur. See how to set up a notification system which does that in this episode.
Note: I forgot to mention, the exception_logger plugin requires will_paginate, so make sure you have that installed either through a plu ...
- 2008-12-12 11:48
- 浏览 712
- 评论(0)
Sometimes you need to display an administrative announcement to every page on the site and give the users the ability to hide the announcement. See how in this episode.
script/generate scaffold announcement message:text starts_at:datetime ends_at:datetime
script/generate controller javascripts
&l ...
- 2008-12-12 11:42
- 浏览 833
- 评论(0)
Usually a select menu is used for setting a belongs_to association, but in this episode I will show you how to use a text field with auto completion.
script/plugin install auto_complete
# product.rb
def category_name
category.name if category
end
def category_name=(name)
self.category = ...
- 2008-12-12 11:34
- 浏览 709
- 评论(0)
If you have complex view logic, this can easily lead to helper methods which call each other. See how to refactor this out into another object in this episode.
# application_helper.rb
def render_stars(rating)
StarsRenderer.new(rating, self).render_stars
end
# helpers/stars_renderer.rb
class ...
- 2008-12-12 11:31
- 浏览 640
- 评论(0)
Tip #1: Whitespace in ERB Templates
Use a dash at the beginning and end of an ERB tag to remove the white space around it.
<div id="products">
<%- for product in @products -%>
<h3><%=h product.name %></h3>
<%- end -%>
</div>
Tip #2: co ...
- 2008-12-11 21:20
- 浏览 600
- 评论(0)
How do you handle partials which have differences depending on the action which is rendering them? Here's three suggestions for this problem.
<!-- index.html.erb -->
<% for article in @articles %>
<% render :layout => 'article', :object => article do %>
<p><% ...
- 2008-12-11 21:12
- 浏览 754
- 评论(0)
You can use profiling to determine where the performance bottlenecks are in specific Rails actions. Watch this episode for details.
# config/environments/staging.rb
config.cache_classes = true
config.action_controller.consider_all_requests_local = false
config.action_controller.perform_caching ...
- 2008-12-11 21:08
- 浏览 607
- 评论(0)
Git has been getting a lot of buzz lately, and for good reason. It's an excellent SCM which in many ways is more simple and powerful than subversion. See how to set up a Rails project with Git in this episode.
# commands
git init
git status
git add .
git commit -a
touch tmp/.gitignore log/.gitig ...
- 2008-12-11 20:29
- 浏览 701
- 评论(0)
See how to handle authentication and custom actions using ActiveResource in this episode.
# models/product.rb (in blog app)
class Product < ActiveResource::Base
self.site = "http://admin:secret@localhost:3000"
end
# script/console (in blog app)
Product.find(:all) # GET products. ...
- 2008-12-11 20:27
- 浏览 614
- 评论(0)