Recent notes
RSS feed![Default_avatar_30](https://www.gravatar.com/avatar/7a23526e8de7f2fa57d2481b549b6d57?default=http://apidock.com/images/default_avatar_30.png&size=30)
Ajax form
<% form_remote_tag :url => { :action => “analyze”}, :update => “result” do %>
<%= select_tag 'company_id', options_for_select([]) %><br/> <%= text_area_tag :text, nil, { :cols => 100, :rows => 10 }%><br/> <%= submit_tag "Analyze", :disable_with => "Please wait..." %> <% end %> <div id="result"></div>
![Default_avatar_30](https://www.gravatar.com/avatar/f9956cc234ccacfee8d107bde748157c?default=http://apidock.com/images/default_avatar_30.png&size=30)
:use_route to force named routes in url_for
If you are using a plugin or library that calls url_for internally, you can force it to use a particular named route with the :use_route key. For instance, calling:
url_for(:controller => 'posts', :action => 'view', :id => post, :use_route => :special_post)
will have the same effect as:
special_post_url(post)
Naturally, this is much more verbose if you’re calling it directly, but can be a lifesaver if url_for is being called inside another method (e.g. will_paginate).
![Default_avatar_30](https://www.gravatar.com/avatar/e9e852b18079792bc5f4ec52b8e37c53?default=http://apidock.com/images/default_avatar_30.png&size=30)
Re: Using a Loading Graphic
You probably want to be using :complete, not :loaded, to execute Javascript when an Ajax request has finished. See: http://prototypejs.org/api/ajax/options
![Default_avatar_30](https://www.gravatar.com/avatar/a9bfdd0cc75c857b669c37548b8bfdf9?default=http://apidock.com/images/default_avatar_30.png&size=30)
Using your model's connection
It’s possible to execute raw SQL over the currently established connection for a model.
You may configure Rails to use different databases for different models. To make sure you are querying the correct database you may do the following:
MyModel.connection.execute("UPDATE `my_models` SET `beer`='free' WHERE 1")
![Default_avatar_30](https://www.gravatar.com/avatar/6c85cc2920423530609e7d80d6b2d17f?default=http://apidock.com/images/default_avatar_30.png&size=30)
Common options
“Common options” mentioned here is default PrototypeHelper options documented in link_to_remote
This means you can use :loading, :loaded, :failure, :success, etc in observe_field.
![Default_avatar_30](https://www.gravatar.com/avatar/2101d75ccd71c5dfb984991a7ba53b9b?default=http://apidock.com/images/default_avatar_30.png&size=30)
Parsing YAML from a POST request
When building a REST server which should accept YAML there are several things to take into consideration.
First of the client should tell the server what type of data it is going to send. This is done via the Content-Type header (which is NOT only a response header as opposed to what the RESTful Web Services book from O’Reilly made us believe).
Second the server application should know how handle the body of the POST request. Placing the following line in your environment.rb:
ActionController::Base.param_parsers[Mime::YAML] = :yaml
This registers the YAML parser. Smooth sailing from here on!
![Default_avatar_30](https://www.gravatar.com/avatar/7a23526e8de7f2fa57d2481b549b6d57?default=http://apidock.com/images/default_avatar_30.png&size=30)
Add spacer template
<%= render :partial => “product”, :collection => @products, :spacer_template => “product_ruler” %>
![Default_avatar_30](https://www.gravatar.com/avatar/afe668d3b3fd174346c28639c8a4aea2?default=http://apidock.com/images/default_avatar_30.png&size=30)
![Default_avatar_30](https://www.gravatar.com/avatar/59436ecd4fe6ad7c34f67654d839f05f?default=http://apidock.com/images/default_avatar_30.png&size=30)
current_url
exact url from browser window:
def current_url url_for :only_path=>false,:overwrite_params=>{} end
![Default_avatar_30](https://www.gravatar.com/avatar/927000b0375176b43d72d0f14d066599?default=http://apidock.com/images/default_avatar_30.png&size=30)
Compiling mysql gem in Leopard with MacPorts MySQL
Needs architecture and reference to mysql_config:
sudo env ARCHFLAGS="-arch i386" gem install mysql -- \ --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
![Default_avatar_30](https://www.gravatar.com/avatar/2101d75ccd71c5dfb984991a7ba53b9b?default=http://apidock.com/images/default_avatar_30.png&size=30)
Rendering YAML
When you want to render XML or YAML you can use
render :xml, some_object.to_xml
or
render :json, some_object.to_json
However there is no equivalent for YAML. What you can do is render just plain text with a correct content-type:
render :text => some_object.to_yaml, :content_type => 'text/yaml'
The content_type is debatable but this seems to be the most standard.
![Default_avatar_30](https://www.gravatar.com/avatar/9338c9a8ebddbcd4035280e6d0d52c31?default=http://apidock.com/images/default_avatar_30.png&size=30)
re: james' note incorrect
kieran is correct, my note is incorrect, it was not meant for ActionMailer::Base
![Default_avatar_30](https://www.gravatar.com/avatar/74198b06bc9e68739debfe6c23e070d3?default=http://apidock.com/images/default_avatar_30.png&size=30)
james' note incorrect
The render method in ActionMailer is infact a private method, in all versions (including the new Rails 2.2).
However, spectators note about @template works well. Thanks.
![Default_avatar_30](https://www.gravatar.com/avatar/9338c9a8ebddbcd4035280e6d0d52c31?default=http://apidock.com/images/default_avatar_30.png&size=30)
RERE: 1 render definition for many actions (cleaner)
@arronwashington. Ah, so that’s the motivation for using default_render, thanks for clearing that up, thanks :)
![Default_avatar_30](https://www.gravatar.com/avatar/fb436f386c71b1c22d31f2d8504e5574?default=http://apidock.com/images/default_avatar_30.png&size=30)
RE: 1 render definition for many actions (cleaner)
Hi James,
Unfortunately that doesn’t work if you use the
@zombies
variable in the rendered template, which is why default_render was added as a patch AFAIK. :)
before_filter is called too soon, after_filter is called after an attempt at rendering is made, so default_render is the only option for this as far as I know.
![Default_avatar_30](https://www.gravatar.com/avatar/9338c9a8ebddbcd4035280e6d0d52c31?default=http://apidock.com/images/default_avatar_30.png&size=30)
re: Specifying :include no longer necessarily joins the association
I have seen how :include does not nessisarily perform a join during that SQL query, if you need the join to occur then, rather then tricking AR (“forcing”), use :joins instead of :include to ensure the joins occur.
![Default_avatar_30](https://www.gravatar.com/avatar/9338c9a8ebddbcd4035280e6d0d52c31?default=http://apidock.com/images/default_avatar_30.png&size=30)
1 render definition for many actions (cleaner)
@arronwashington, you can just call render rather then using instance_eval to overwrite default_render, for example:
before_filter :render_filter, :only => [:zombies, :cool_zombies] def zombies @zombies = Zombie.find(:all) end def cool_zombies @zombies = Zombie.find(:all, :conditions => { :eats_humans => false, :is_hippie => true }) end protected def render_filter # without instance_eval or overwriting default_render render :template => 'zombies/all' end
![Default_avatar_30](https://www.gravatar.com/avatar/fb436f386c71b1c22d31f2d8504e5574?default=http://apidock.com/images/default_avatar_30.png&size=30)
1 render definition for many actions.
Overriding default_render in especially useful when you have many actions that all render the same thing.
before_filter :render_filter, :only => [:zombies, :cool_zombies] def zombies @zombies = Zombie.find(:all) end def cool_zombies @zombies = Zombie.find(:all, :conditions => { :eats_humans => false, :is_hippie => true }) end protected def render_filter instance_eval do def default_render render :template => 'zombies/all' end end end
![Default_avatar_30](https://www.gravatar.com/avatar/6dd10772cfa5333c5db924091464ed95?default=http://apidock.com/images/default_avatar_30.png&size=30)
params hash gets the model id automatically
The params hash gets automatically populated with the id of every model that gets passed to form_for. If we were creating a song inside an existing album:
URL:/albums/209/songs/new form_for [@album, @song] do |f| ... f.submit "Add" end
The params hash would be:
params = {"commit"=>"Add", "authenticity_token"=>"...", "album_id"=>"209", "song"=>{"song_attributes"=>{...}} }
So, in the songs_controller you could use this album_id in a before_filter:
before_filter :find_album protected def find_album @album = Album.find(params[:album_id]) end
If you only did this:
form_for @song do |f|
You would get this params hash:
params = {"commit"=>"Add", "authenticity_token"=>"...", "song"=>{"song_attributes"=>{...}} }
![Default_avatar_30](https://www.gravatar.com/avatar/6dd10772cfa5333c5db924091464ed95?default=http://apidock.com/images/default_avatar_30.png&size=30)
The reverse operation of split is join.
Given that String#split returns an array, its reverse operation is Array#join. Example:
"life is awesome".split =>["life","is","awesome"] ["life","is","awesome"].join(" ") =>"life is awesome"
![Default_avatar_30](https://www.gravatar.com/avatar/7a23526e8de7f2fa57d2481b549b6d57?default=http://apidock.com/images/default_avatar_30.png&size=30)
By images's sub dirctionary to img tag
image_tag(“icons/edit.png”) # =>
<img src="/images/icons/edit.png" alt="edit" />
![Default_avatar_30](https://www.gravatar.com/avatar/9224d72c1e25c1781fca98456d69f716?default=http://apidock.com/images/default_avatar_30.png&size=30)
re: Customizing attribute names in error messages
You can use Module.alias_attribute to achieve the same result as overriding human_attribute_name. After aliasing use the new name in the validates_xxxx_of methods or ActiveRecord::Errors.add.
![Default_avatar_30](https://www.gravatar.com/avatar/2fb97d9bab9b2ddf4979e3ea1e5d6ea5?default=http://apidock.com/images/default_avatar_30.png&size=30)
Anyone know the order the scopes assemble conditions?
It seems like last scope = first condition in sql. Can anyone confirm?
![Default_avatar_30](https://www.gravatar.com/avatar/405c75b3c2ae3087b30956b21d250765?default=http://apidock.com/images/default_avatar_30.png&size=30)
can be useful to achieve attr_reader effect
e.g.
class Account < ActiveRecord::Base def credit(amount) self.write_attribute(:balance, self.balance + amount) self.save! end def balance=(value) raise "You can't set this attribute. It is private." end end
this allows fred.account.credit(5) whilst raising an error on fred.account.balance = 1000
![Default_avatar_30](https://www.gravatar.com/avatar/175b1b15a0ae5a8a690479641ad8ac5a?default=http://apidock.com/images/default_avatar_30.png&size=30)
Can be extended but only with a module
Although not documented, belongs_to does support Association Extensions however it doesn’t accept a block like has_many does. So you can’t do this:
class Account < ActiveRecord::Base belongs_to :person do def do_something_funky # Some exciting code end end end
but you can do this:
module FunkyExtension def do_something_funky # Some exciting code end end class Account < ActiveRecord::Base belongs_to :person, :extend => FunkyExtension end
And then call it like this:
@account = Account.first @account.person.do_something_funky
![Default_avatar_30](https://www.gravatar.com/avatar/b436ee8a3631036ff23f7535848cc8f5?default=http://apidock.com/images/default_avatar_30.png&size=30)
Does very litle
This method simply returns the hash itself since HashWithIndifferentAccess by definition support symbolized key access. For a (regular rails) Hash there is ActiveSupport::CoreExtensions::Hash::Keys#symbolize_keys! that “Destructively convert all keys to symbols”.
![Default_avatar_30](https://www.gravatar.com/avatar/6cf87365e668b95f086c1348c5c35bdc?default=http://apidock.com/images/default_avatar_30.png&size=30)
A work-around for adding confirmation to image_submit_tag
Sometimes you may want to add a confirmation to image submit tags but this function does not allow it. To get over this limitation use a normal submit tag and set the src and type properties (set type to “image”)
Code example
submit_tag “Delete”, :confirm => “Are you sure?”, :src => “/images/trash.png”, :type => “image” %>
![Default_avatar_30](https://www.gravatar.com/avatar/59436ecd4fe6ad7c34f67654d839f05f?default=http://apidock.com/images/default_avatar_30.png&size=30)
Also for numeric
1.humanize == “1″ 1000000.humanize == “1.000.000″ 1000.12345.humanize == “1.000,12″
http://pragmatig.wordpress.com/2008/10/25/numbers-for-humans-humanize-for-numeric/
![Default_avatar_30](https://www.gravatar.com/avatar/59436ecd4fe6ad7c34f67654d839f05f?default=http://apidock.com/images/default_avatar_30.png&size=30)
Alternative: use 1000.humanize
1.humanize == “1″ 1000000.humanize == “1.000.000″ 1000.12345.humanize == “1.000,12″
http://pragmatig.wordpress.com/2008/10/25/numbers-for-humans-humanize-for-numeric/
![Default_avatar_30](https://www.gravatar.com/avatar/2fb97d9bab9b2ddf4979e3ea1e5d6ea5?default=http://apidock.com/images/default_avatar_30.png&size=30)
module includes with callbacks
If you write a plugin or module that includes callbacks make sure to define the method and call super after you’re done with your business.
module CoolStuff
def self.included(base) super base.extend(ClassMethods) # the next line seems to clobber. instead opt for defining an inheritable method # base.after_save :chill end module ClassMethods # cool class methods end def chill self.cool = true end def after_save self.chill super # if you don't call super, bloggy won't run end
end # yes I know this next line is a divisive issue but it’s common enough ActiveRecord::Base.send :include, CoolStuff
class Blog < ActiveRecord::Base
after_save :bloggy def bloggy slugify_title end
end