Nested Object Forms with has_one Relation

Rails 2.3 has a good solution for multi model forms. But there is not much examples of  how to use accepts_nested_attributes_for with has_one relation.

Assume that we have a Book model which has one Author.



Controller :

View:


No special action is needed for create method of BooksController,  attr_accessible is not necessary for Book model. Just don’t forget to add the second parameter (@book) of form_for and build your nested object (@book.build_author).

Tidbit: Converting Strings with Line Feed to Array

If you have trouble when converting string to array, check if your string includes any line feed (\n). Check how Array("string") behaves:

irb(main):001:0> Array("izzet emre \nkutlu")
=> ["izzet emre \n", "kutlu"]

This output is not what most of the people expected. Be careful!

WorkingWithRails WordPress Plugin

A simple plugin which displays Working With Rails recommendation badge.

  1. Download the plugin.
  2. Extract the plugin to your wordpress plugin directory.
  3. Activate the plugin.
  4. Add the widget to your sidebar.
  5. Enter your information.

It’s version is 0.1 so comments will be appreciated.

Rails 2.2 & Inflector

If you have recently updated to Rails 2.2.2, you may encounter this error when you want to start your application:

/.gem/ruby/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:445:in
`load_missing_constant': uninitialized constant Inflector (NameError)

As I learned from Paul’s post usage of Inflector class is changed a bit. You can see the difference when you compare the inflections.rb files. Path of the file is yourApp/config/initializers/inflections.rb

inflections.rb (Rails 2.1.0)

 Inflector.inflections do |inflect|
  .
  .
  .
 end

inflections.rb (Rails 2.2.2)

 ActiveSupport::Inflector.inflections do |inflect|
  .
  .
  .
 end

In my situation changing Inflector to ActiveSupport::Inflector was enough to solve the problem.

Updating RubyGems to 1.2 (Manually)

This is what happened when I want to install rmagick gem.

username@username-desktop:~$ sudo gem install rmagick
Bulk updating Gem source index for: http://gems.rubyforge.org/
ERROR:  could not find rmagick locally or in a repository

What the hell…
After some googling I found that RubGems 1.1.x is buggy and and update to RubyGems 1.2 is necessary.
To learn your RubyGems version:

username@username-desktop:~$ gem -v
1.1.0

Click here to read more »

Recursive methods with block in Ruby

def comic(cast)
   cast.each do |character|
      unless character.is_a?(Array)
         yield(character)
      else
	 comic(character) {|x| yield x}
      end
   end
end

This method takes an array as argument and checks each element if it is an Array.
If the element is not an Array then the block is called.
If the element is an Array then the same method is called with that element as an argument and with the same block.

Now let’s look how we can call this method.

names = ['lucky luke', 'jolly jumper', 'rin tin tin', ['joe', 'william','jack', 'averell']]
comic(names) {|c| puts c}

Output :

lucky luke
jolly jumper
rin tin tin
joe
william
jack
averell

How to check if a div exists in rjs?

<body>
   <div id='main'>
      Main Div
   </div>
</body>

render :update do |page|
   page << "if ($('main')){"
   page.replace_html 'main', :inline => 'Here it is'
   page << "}"
end

This ruby code checks if there is a DOM object that its id = “main”.
If there is then changes its content to “Here it is”, if there is not do nothing.