Notes posted to Ruby on Rails
RSS feedUse this in controllers
Sometimes you’re gonna need this in controllers. Just put this in the controller:
include ActionView::Helpers::NumberHelper
:find takes more keys than written
The documentation says that the :find keywords “may include the :conditions, :joins, :include, :offset, :limit, and :readonly options”. Note that this does not mean that only those options are supported. :sort also works like it should, for example.
Will this method get rid of existing data?
Will this method get rid of existing data?
Default fallback
You can specifly :default option which is useful when the translation is not found. For example:
t(:this_translation_doesnt_exist, :default => 'Ooops!') # => Ooops!
Or even any number of “fallbacks” - the first not nil is returned:
t(:missing, :default => [:missing_too, :existing, 'Sad panda']) # => :existing translation
Good introduction to Rails I18n is http://guides.rubyonrails.org/i18n.html
Status Codes
Full detail: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
-
100 - Continue
-
101 - Switching Protocols
-
200 - OK
-
201 - Created
-
202 - Accepted
-
203 - Non-Authoritative Information
-
204 - No Content
-
205 - Reset Content
-
206 - Partial Content
-
300 - Multiple Choices
-
301 - Moved Permanently
-
302 - Found
-
303 - See Other
-
304 - Not Modified
-
305 - Use Proxy
-
306 - No Longer Used
-
307 - Temporary Redirect
-
400 - Bad Request
-
401 - Not Authorised
-
402 - Payment Required
-
403 - Forbidden
-
404 - Not Found
-
405 - Method Not Allowed
-
406 - Not Acceptable
-
407 - Proxy Authentication Required
-
408 - Request Timeout
-
409 - Conflict
-
410 - Gone
-
411 - Length Required
-
412 - Precondition Failed
-
413 - Request Entity Too Large
-
415 - Unsupported Media Type
-
416 - Requested Range Not Satisfiable
-
417 - Expectation Failed
-
500 - Internal Server Error
-
501 - Not Implemented
-
502 - Bad Gateway
-
503 - Service Unavailable
-
504 - Gateway Timeout
-
505 - HTTP Version Not Supported
Doesn't return nil if the object you try from isn't nil.
Note that this doesn’t prevent a NoMethodError if you attempt to call a method that doesn’t exist on a valid object.
a = Article.new a.try(:author) #=> #<Author ...> nil.try(:doesnt_exist) #=> nil a.try(:doesnt_exist) #=> NoMethodError: undefined method `doesnt_exist' for #<Article:0x106c7d5d8>
This is on Ruby 1.8.7 patchlevel 174
Attribute names are Strings, not Symbols
Another possible gotcha – the returned hash keys are of type String, not Symbol:
user.attributes["login"] # => "joe" user.attributes[:login] # => nil
expire_page outside controller and sweepers
If you want to expire cached pages from scripts or console just use class-method expire_page. But don’t forget about difference between instance and class method. In class-method you pass page url, not a hash of action/controller:
ActionController::Base.expire_page your_page_url
Expire cache from console or whatever...
I know only one possible method:
ApplicationController.new.expire_fragment(:your_fragment)
Version Ranges
To specify a version range, use array syntax like this:
config.gem 'paperclip', :version => ['>= 2.3.1.1', '< 3.0']
The example will, of course, match any version 2.3.1.1 or newer up until (not including) 3.0 or later.
What's difference between create and new?
What’s difference between create and new?
Capturing blocks after >2.2
After 2.2, you can omit the do …end block and simply use the &block variable directly:
concat(content_tag(:div, :class => "wrapped_content") do capture(&block) end, block.binding)
becomes simply:
concat(content_tag(:div, capture(&block), :class => "wrapped_content"))
IE GOTCHA - multiple javascript_include_tags with cache => true
If you have multiple lines of javascript_include_tag ‘jsfile’, :cache => true, IE does not load them all (though it seems Firefox and Safari do). And the error won’t show up until you’re in production (since that’s only when caching kicks in.)
You should include them all on one line:
javascript_include_tag 'file1.js', 'file2.js', 'file3.js', :cache => 'myfiles'
Clearing out previous values from content_for
By default, content_for :thing appends whatever you put in your block to the previous value of :thing. In some cases, you’d like to clear out :thing rather than append to it.
I just posted a way to do this on my blog: http://stevechanin.blogspot.com/2009/11/clearing-out-content-in-contentfor.html
I add a new method set_content_for that works just like content_for, but clears out :thing first. It doesn’t touch how content_for works, so it shouldn’t cause any problems anywhere else in your app.
You can specify the format as well
You can also specify the format (in case you need to redirect a request coming in one format to another format):
redirect_to :action => 'show', :format => 'html'
How to check if a Yield has content?
How do I do this without actually calling the yield?
- if yield :footer = yield :footer - else = render "layouts/footer_big"
(Note: HAML Syntax) Thanks.
How to use with HAML
Are you using HAML and try to do the block-thing (do…)? Please note that the whole block has to be in a single line. More: http://groups.google.com/group/haml/browse_thread/thread/52e62ef501c504a3
Streaming Does Not Work with Mongrel
If you are trying to stream output via render :text => Proc and Mongrel, be sure to note that this does NOT work. Mongrel returns a StringIO, which by nature buffers everything.
Unsure of how to actually stream output with Rails in a consistent fashion.
assert_response(:success) checks if the status code is in the range 200-299
success? in ActionController::TestResponseBehavior is defined as:
def success? (200..299).include?(response_code) end
Be careful with float ranges
Pay close attention to the fact that the object passed to :in must be enumerable.
If you want to validate a ranking, the following won’t work:
validates_inclusion_of :rating, :in => (0.0..10.0)
Instead, you’ll want to use validates_numericality_of like this:
validates_numericality_of :rating, :greater_than_or_equal_to => 0.0, :less_than_or_equal_to => 10.0
More Docs and Explanation
You probably want to look at the class level docs
http://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods
(cut and paste, apidocks can’t render the above for some reason)
The docs are in the base class
Look in ActionController::Base for the docs.
style for select
<%= select(“post”, “person_id”, Person.all.collect {|p| [ p.name, p.id ] }, {}, :style => “width:100px” %>
Careful with scopes
Just like find, find_in_batches introduces an implicit scope into the block. So for example
Person.find_in_batches(:conditions => {:birthday => Date.today}) do |birthday_childs| Person.all.each do |person| person.send_presents_to(birthday_childs) end end
does not work as expected, because the Person.all within the block will also find only persons with :conditions => {:birthday => Date.today}.


