- 浏览: 96905 次
- 性别:
- 来自: 成都
-
最新评论
-
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
文章列表
When a validation error occurs, Rails helpfully wraps the field in a div tag so you can style it. But sometimes you don't want this behavior. In this episode you will see how to customize it. environment.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag|
"<span cl ...
- 2008-12-04 10:34
- 浏览 606
- 评论(0)
If you have a form with multiple buttons, you can detect which button was clicked by checking the passed parameters. Learn how in this episode.
<!-- projects/new.rhtml -->
<% if params[:preview_button] %>
<%= textilize @project.description %>
<% end %>
...
<%= submit ...
- 2008-12-04 10:27
- 浏览 758
- 评论(0)
A search form is quite different than other forms, this is because it does not deal with model's attributes. See a good way to add a simple search form in this episode.
<!-- projects/index.rhtml -->
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_ ...
- 2008-12-04 10:25
- 浏览 717
- 评论(0)
This episode will walk you through setting up a Rails project on subversion. It covers some helpful tips and gotchas you may experience along the way.
mkdir svn_setup
cd svn_setup/
rails blab
cd blab
mv config/database.yml config/database_example.yml
rm -r log/*
rm -r tmp/*
cd ..
mv blab tru ...
- 2008-12-04 10:09
- 浏览 615
- 评论(0)
REST adds many constraints. It restricts your controllers to seven actions. Normally this is okay, but sometimes you need to add your own custom actions. Learn how in this episode.
map.resources :tasks, :collection => { :completed => :get }, :member => { :complete => :put }
<!-- tasks ...
- 2008-12-04 10:06
- 浏览 672
- 评论(0)
#34 Named Routes
- 博客分类:
- Railscasts
When you add a custom route, make it a named route so you have url helper methods to easily link to that new route. See episode for details.
# routes.rb
map.resources :projects
map.task_archive 'tasks/:year/:month', :controller => 'tasks', :action => 'archive'
map.home '', :controller => ...
- 2008-12-04 10:04
- 浏览 639
- 评论(0)
You can sometimes remove a lot of duplication by generating methods dynamic. In this episode I will show you how to create a plugin which does exactly that.
# models/task.rb
stringify_time :due_at
def validate
errors.add(:due_at, "is invalid") if due_at_invalid?
end
# stringify_t ...
- 2008-12-03 23:11
- 浏览 648
- 评论(0)
Although Rails does allow you to edit time attributes with text fields, it's not very flexible. In this episode you will learn how to use a virtual attribute to format the time to your liking.
<!-- tasks/_form.rhtml -->
<%= f.text_field :due_at_string %>
# models/task.rb
def due_at_str ...
- 2008-12-03 23:05
- 浏览 604
- 评论(0)
Learn how to use the trusty strftime method to format a time, and see how Rails allows you to save this format for later use.
# config/environment.rb
Time::DATE_FORMATS[:due_time] = "due on %B %d at %I:%M %p"
<%= task.due_at.to_s(:due_time) %>
- 2008-12-03 21:18
- 浏览 382
- 评论(0)
If you are like me, you avoid creating page titles because it is kind of a pain. But in this episode I will show you a clean way to add titles to your pages.
# application_helper.rb
def title(page_title)
content_for(:title) { page_title }
end
- 2008-12-03 21:14
- 浏览 653
- 评论(0)
#29 group_by Month
- 博客分类:
- Railscasts
Learn how to use the very useful group_by method to group an array by anything you want! In this episode I group an array of tasks by month then sort it properly.
# tasks_controller.rb
def index
@tasks = Task.find(:all, :order => 'due_at, id', :limit => 50)
@task_months = @tasks.group_by ...
- 2008-12-03 20:48
- 浏览 628
- 评论(0)
Have you ever wanted to visually line up items in rows and columns? The in_groups_of method makes this a cinch. Just watch out for the gotcha.
<!-- tasks/index.rhtml -->
<table>
<% @tasks.in_groups_of(4, false) do |row_tasks| %>
<tr>
<% for task in row_tasks %> ...
- 2008-12-03 20:16
- 浏览 657
- 评论(0)
One of the most common security problems for dynamic sites is SQL Injection. Thankfully Rails does everything it can in solving this issue, but you still need to be aware of it.
# tasks_controller.rb
def index
@tasks = Task.find(:all, :conditions => ["name LIKE ?", "%#{params[:q ...
- 2008-12-03 19:43
- 浏览 667
- 评论(0)
If you need to display the record count for a has_many association, you can improve performance by caching that number in a column.
# migrations/006_add_tasks_count.rb
def self.up
add_column :projects, :tasks_count, :integer, :default => 0
Project.reset_column_information
Project.find( ...
- 2008-12-03 13:01
- 浏览 761
- 评论(0)
One way to improve performance is to cut down on the number of SQL queries. You can do this through eager loading. Learn all about it in this episode!
Task.find(:all, :include => :projects)
Task.find(:all, :include => [:projects, :comments])
- 2008-12-03 12:56
- 浏览 689
- 评论(0)