My idiotic encounter with accepts_nested_attributes_for in Rails 3

I was using accepts_nested_attributes_for for the first times in Rails 3 to create forms that have fields from a parent model and its child model. I made a number of mistakes that make me feel like an idiot right now. Actually, the last one did but I saw it fit to blog about it in case anyone else finds themselves wasting an hour or more trying to get their forms to work.

N/B

  • I was using Rails 3.0.5 and Ruby 1.8.7 (MRI)
  • The Parent model had a has_one relationship to the child. (This shouldn’t matter though)

Mistake 1

In this case, my fields_for helper wasn’t evaluating to anything. The textfields and textareas were therefore not being created in the view. The issue was that in Rails 3, the helpers form_for and fields_for use <%= instead of <% !! I used the latter. (i)

Therefore, my form_for and fields_for helpers should have looked like this:

<%= form_for ......... %>
<%= fields_for ......... %>

This made the text fields visible but the values weren’t there.

Mistake 2

I didn’t make the attributes of the child model accessible to the parent model. This is done using attr_accessible and passing child_attributes.

e.g

def Dog < ActiveRecord::Base
has_one :puppy
accepts_nested_attributes_for :puppy
attr_accessible: puppy_attributes
end

Mistake 3 (Epic Fail)

Now this is the part that got me cursing all over the place. My form was being created using the variable from the controller rather than a symbol i.e

<%= form_for @dog do |f| %>

INSTEAD OF

<%= form_for :dog do |f| %>

The field_for was supposed to accept an object of the puppy model referenced from the @dog variable passed to the form. Problem is I was so frustrated by the fact that my text fields were empty that I started binging on Google search results and ended up with a fields_for that accepted the object of the puppy model as a symbol hahaha! yeah stupid.. i know. This is how it looked:

<%= fields_for :puppy do |puppy_fields| %>
<%= puppy_fields.text_field :breed %>
<% end %>

It obviously should have been like this:

<%= fields_for @dog.puppy do |puppy_fields| %>
<%= puppy_fields.text_field :breed %>
<% end %>

I’m going to cringe away in a dark corner now… Cheers!!

 

“File not found: lib” error while installing Rails 3.0.7

I was installing Rails 3.0.7 on my local machine yesterday and on a server today. I came across the same error both times:

This is the problematic part:

File not found: lib
ERROR:  While generating documentation for rails-3.0.7
... MESSAGE:   exit
... RDOC args: --ri --op /usr/local/rvm/gems/ruby-1.8.7-p334/doc/rails-3.0.7/ri lib --title rails-3.0.7 Documentation --quiet

Rails installed successfully but the Rdoc and ri documentation failed with this error. I ignored it in my local machine since I was in a hurry and Rdoc/ri were the least of my worries. But when it hapenned again in the server I just had to find out what the issue was. If you google it, many sources advise you to just uninstall that Rails version (in my case 3.0.7) and do this:

sudo gem install rails –no-ri –no-rdoc

I find that to be plain stupid! You are running away from the problem rather than solving it. The above command would just ignore the Rdoc/ri documentation.

The issue is that the new Rails version you are installing uses a more recent version of Rdoc/ri and thus when the installation process gets to Rdoc/ri installation, it fails. I don’t know why the ROR developers didn’t think of checking for this and updating Rdoc/ri for you. Anyway, the correct way to solve this issue is to first uninstall the Rails version you just installed with no Rdoc/ri data:

sudo gem uninstall rails –version 3.0.7

Install the latest version of the rdoc-data gem. This will install ri data for core and stdlib:

gem install rdoc-data

Then run:

rdoc-data –install

In case you want rdoc-data for all your gems run (not necessary.. takes a while if you have as many gems as i do):

gem rdoc –all –overwrite

To install ri data for RDoc 2.5+ run:

rdoc-data

Now you can install your Rails version and it will successfully install all the rdoc-data too.

sudo gem install rails

Output:

Successfully installed rails-3.0.7
1 gem installed
Installing ri documentation for rails-3.0.7...
Installing RDoc documentation for rails-3.0.7...

Installing mysql2 on rails3 – Error installing mysql2: ERROR: Failed to build gem native extension

I’m posting this because i spent a lot of time trying to solve this little problem. First of all, I should let you know that I’m working in Ubuntu 10.04 with Rails 3.0.0 installed. I couldn’t do anything on Rails that would depend on MySQL. I just had to install the mysql2 gem but my initial attempts proved futile! Error messages:

Building native extensions.  This could take a while…
ERROR:  Error installing mysql:
ERROR: Failed to build gem native extension.

SOLUTION:

1. Install the mysql library for ruby and the development libraries

sudo apt-get install libmysql-ruby libmysqlclient-dev

2. Now you can install the mysql2 gem

sudo gem install mysql — –with-mysql-lib=/usr/lib/mysql/lib

This time the mysql2 gem installed successfully!!! After wasting very many precious minutes!!! #RANT!!