Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

http://installingcats.com

writing destructive string methods in Ruby

Use string class' method "replace" to overwrite its "self" value.

Example:

  def decapitalize!
    replace (self.reverse.chop + self.reverse.last.downcase).reverse
  end
  
  def decapitalize
    dup.decapitalize!
  end

Two or more parameters in RESTful route

Regardless of whether this is RESTful, to add multiple parameters to a normally RESTful route use the following

myresource_path(:id => myId, :extra_param => extraId, :extra_param2 => blah)


Results in
myresource/myId?extra_param=extraId&extra_param2=blah


See this Google Rails Group thread from Jeremy Kemper.

SOAP4R Rails NameError FIX

// description of your code here

Update /Library/Ruby/Gems/1.8/gems/soap4r-1.5.8/lib/soap
  # KNOWN_TAG = XSD::NS::KNOWN_TAG.dup.update(
  #   SOAP::EnvelopeNamespace => 'env'
  # ) # Replaced with code from http://dev.ctor.org/soap4r/ticket/433
  KNOWN_TAG = {
      XSD::Namespace => 'xsd', XSD::InstanceNamespace => 'xsi', SOAP::EnvelopeNamespace => 'env'
  }

Freeze rails

Freeze rails 1.2.3 to a project through rubyonrails.org TRAC.

rake rails:freeze:edge TAG=rel_1-2-3

old deprecated legacy plugins repository for Rails

// description of your code here

Find old official rails trunk plugins that didn't make the cut here:

http://dev.rubyonrails.org/svn/rails/plugins/legacy/

rails console - add module methods

Extend the functionality of the rails console by mixing in modules

>> extend ERB::Util
=> #<Object:0x2572f68>
>> h('<b>blahblah</b>')
=> "&lt;b&gt;blahblah&lt;/b&gt;"


You can extend ActionView::Helpers, etc. with this to provide view method functionality in the console.

http://errtheblog.com/post/43

eliminate duplicate flash on before_filter methods

When using a before filter to prevent unauthorized access to methods, use flash.now instead of flash, when painting error messages.

flash.now[:warning] = "You must be the group admin (and logged in) to edit."


see KerryBuckley.com for more info

ruby_inline permission denied when starting mongrel_cluster from init.d

The cause is the project is trying to read /root/.ruby_inline/Inline_ImageScience_aa58.c, yet the user running the rails project is the one specified in the /config/mongrel_cluster.yml (usually some other user that is NOT root).

When the project references anything to do with ImageScience, it will complain that it has insufficient rights to read the c library file, of course, since it's in root's home dir.

Solution, specify a home environment variable in your rails project's production.rb file (/rails_project/config/environments/production.rb)

ENV['HOME'] = '/home/billy'


Now when the project is started from system boot, or manually by calling ./etc/init.d/mongrel_cluster start, the rails project will always look to the user's home directory, rather than root's.

EDIT:

Or better yet, use this patch to lib/mongrel/configurator.rb
(was posted at ruby-on-rails talk google groups)
-          log "Changing user to #{user}." 
-          Process::UID.change_privilege(Etc.getpwnam(user).uid)
+          log "Changing user to #{user}."
+          getpwnam = Etc.getpwnam(user)
+          Process::UID.change_privilege(getpwnam.uid)
+          ENV['HOME'] = getpwnam.dir

Rotate Rails Log files

// description of your code here
Keeps 50 files of a maximum size of 5MB each

config.logger = Logger.new("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", 50, 5242880)

field focus in rails template

Stick the following in application_helper.rb:

def set_focus_to_id(id)
  javascript_tag("$('#{id}').focus()");
end


Then this in the templates as needed to generate a javascript focus tag.
(Note this exaple pretends to set focus for a field with id='user_login').
<%= set_focus_to_id 'user_login' %>