Recent good notes
RSS feed![Default_avatar_30](https://www.gravatar.com/avatar/54f0de0b0bdee7e045c5a128c6ffeaf2?size=30&default=http://apidock.com/images/default_avatar_30.png)
Rails defined Mime Types
Here are all the default Rails Mime Types:
"*/*" => :all "text/plain" => :text "text/html" => :html "application/xhtml+xml" => :html "text/javascript" => :js "application/javascript" => :js "application/x-javascript" => :js "text/calendar" => :ics "text/csv" => :csv "application/xml" => :xml "text/xml" => :xml "application/x-xml" => :xml "text/yaml" => :yaml "application/x-yaml" => :yaml "application/rss+xml" => :rss "application/atom+xml" => :atom "application/json" => :json "text/x-json" => :json
![Default_avatar_30](https://www.gravatar.com/avatar/54f0de0b0bdee7e045c5a128c6ffeaf2?size=30&default=http://apidock.com/images/default_avatar_30.png)
Default Mime Types
This module sets up all the default mime-types. Here they are:
"*/*" => :all "text/plain" => :text "text/html" => :html "application/xhtml+xml" => :html "text/javascript" => :js "application/javascript" => :js "application/x-javascript" => :js "text/calendar" => :ics "text/csv" => :csv "application/xml" => :xml "text/xml" => :xml "application/x-xml" => :xml "text/yaml" => :yaml "application/x-yaml" => :yaml "application/rss+xml" => :rss "application/atom+xml" => :atom "application/json" => :json "text/x-json" => :json
![Default_avatar_30](https://www.gravatar.com/avatar/f1bdea1eaab174b4e08a46911fc9af96?size=30&default=http://apidock.com/images/default_avatar_30.png)
x-sendfile
Rails 2.1 supports the x_sendfile apache module:
send_file '/path/to.png', :x_sendfile => true, :type => 'image/png'
![Default_avatar_30](https://www.gravatar.com/avatar/aecc642f996c6be3b112c8dd96c0fe7d?size=30&default=http://apidock.com/images/default_avatar_30.png)
Handy for adding theme support
I’m using this to add basic theme support to my app:
append_view_path(File.join(RAILS_ROOT, "app/themes/#{@current_theme}")) append_view_path(File.join(RAILS_ROOT, 'app/themes/_base'))
Common templates go in app/themes/_base and then you can override these with specific theme versions in each theme directory.
![Default_avatar_30](https://www.gravatar.com/avatar/54f0de0b0bdee7e045c5a128c6ffeaf2?size=30&default=http://apidock.com/images/default_avatar_30.png)
How to test different responses in controller tests/specs
When you want to write a controller test or spec (rspec) to test out a different response type other than html, just set the HTTP_ACCEPTS header like so before the request:
@request.env['HTTP_ACCEPT'] = "application/rss" post :create, :blog => {}
![Default_avatar_30](https://www.gravatar.com/avatar/54f0de0b0bdee7e045c5a128c6ffeaf2?size=30&default=http://apidock.com/images/default_avatar_30.png)
Custom MIME Type
After you register a custom Mime::Type like stated above, you can do:
respond_to do |format| # .jpg corresponds to the second argument passed to #register # Mime::Type.register "image/jpg", :jpg format.jpg { ...do something here... } end
![Default_avatar_30](https://www.gravatar.com/avatar/e5c8c978e7736dec9e2f569ace16cf83?size=30&default=http://apidock.com/images/default_avatar_30.png)
:expires_in not in Rails 2.1
AFAIK this is from a patch to Rails 2.1, which hasn’t been accepted yet.
Also, I think it needs to be supported by the cache_store you’re using.
![Default_avatar_30](https://www.gravatar.com/avatar/02f0ee0da223f90bb9996ef252e528c1?size=30&default=http://apidock.com/images/default_avatar_30.png)
collection.exists?(conditions)
The created association method also supports the ‘exists?’ method, similar to ActiveRecord::Base#exists?
has_and_belongs_to_many :categories ... categories.exist?(1) # Check whether there's a relation with a Category # object whose id is 1. categories.exist?(:id => 1) # ditto categories.exist?(['id', 1]) # ditto categories.exist?(:name => 'Anime')
![Default_avatar_30](https://www.gravatar.com/avatar/980caceff69ddad3f9583203923beda7?size=30&default=http://apidock.com/images/default_avatar_30.png)
update_on and update_at only set on attribute change
If you call save, and no attributes are “dirty” (changed), then an update query will not happen against the database, and thus updated_at and updated_on will not be set.
You’ll need to modify at least one field to get updated_at and updated_on to set themselves.
![Default_avatar_30](https://www.gravatar.com/avatar/980caceff69ddad3f9583203923beda7?size=30&default=http://apidock.com/images/default_avatar_30.png)
Bang methods also need will_change!
As it says here at the bottom if you do in-place modifications using << you’ll need to call the will_change method!
This also goes for bang methods. So:
person = Person.first person.name.downcase! person.save
will not save anything! Save will never be called. To get the name saved you need to do
person = Person.first person.name_will_change! person.name.downcase! person.save
This will save the name.
![Default_avatar_30](https://www.gravatar.com/avatar/b13ea9d5d03e9a63ed9a0baabfb25cdc?size=30&default=http://apidock.com/images/default_avatar_30.png)
Set cache time-to-live
You can specify time-to-live for the cached item in seconds with :expires_in option.
class ListsController < ApplicationController caches_action :index, :expires_in => 1.hour end
![Default_avatar_30](https://www.gravatar.com/avatar/535215c41c0949117577517b4506202c?size=30&default=http://apidock.com/images/default_avatar_30.png)
Nil V.S. Empty String HTML Options
There is a difference between an empty string and nil value for options hash.
Code Sample
content_tag( :div, 'Hello World!', :class=>'') # => "<div class="">Hello World!</div>" content_tag( :div, 'Hello World!', :class=>nil) # => "<div>Hello World!</div>"
![Default_avatar_30](https://www.gravatar.com/avatar/40e75e91ba81120383c7223690e9bcc0?size=30&default=http://apidock.com/images/default_avatar_30.png)
Using memcached as a session store
Because of Ruby’s CGI library limitations, store cannot have any configuration options. Basically this means that you cannot easily run memcached on a different port (or with any non-default settings for that matter).
You can bypass this limitation with this ugly hack (environment.rb):
cache_params = *([memcache_servers, memcache_options].flatten) CACHE = MemCache.new(*cache_params) ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!({ 'cache' => CACHE })
In your initializer block, just configure session_store normally:
config.session_store = :mem_cache_store
I think this should be fixed to work like cache_store= does.