`
CaiDeHen
  • 浏览: 96905 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论
文章列表
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 ...
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 ...
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_ ...
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 ...
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 ...

#34 Named Routes

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 => ...
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 ...
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 ...
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) %>
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

#29 group_by Month

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 ...
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 %> ...
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 ...
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( ...
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])
Global site tag (gtag.js) - Google Analytics