Blog

Rails3: ActiveRecord accepts_nested_attributes_for not working (can’tmass-assign protected attributes)

In one of my applications I ran into a situation where I needed to have a single form handle a model as well as several of its associations. Meaning I needed the form to handle CRUD events for the model plus the associated models auto-magically. With that objective I remembered a RailsCast.com video cast explaining how to use the ActiveRecord ‘accepts_nested_attributes_for’ method.

So I jumped on the web and coded along while I watched the cast. But for some strange reason the code from the cast just wouldn’t work. When I saved the survey no associated questions were saved to the database despite the fact that the survey came back as successfully saved. So I triple checked that I had it all correct. Once I was satisfied that it was correct I started digging deeper when I noticed in the passenger logs in my console the following error message hidden nicely within the POST printout and the SQL statements.

<<<>>>

WARNING: Can’t mass-assign protected attributes: questions_attributes

<<<>>>

That made me think, Rails has started doing a much better job protecting our applications from Mass-Assignment attacks by requiring us to explicitly set the model attributes which are accessible and can be directly updated via a form post. I then reviewed the POST parameters to see what was being returned by the form (see below).

<<<>>>

Parameters: {“utf8″=>”✓”, “authenticity_token”=>”8XugXn/UwE+m4m2BR2pfTy7oVfUI+jKnPYaSNwKiD1s=”, “survey”=>{“name”=>”test”, “questions_attributes”=>{“0″=>{“content”=>”question1”}, “1”=>{“content”=>”question2”}, “2”=>{“content”=>”question3”}}}, “commit”=>”Submit”}

<<<>>>

Within the parameter list for the survey sent back by the form I noticed the addition of the ‘questions_attributes’ hash which holds each question with its associated hash of fields and values. Being that the ‘questions_attribute’ is a newly introduced attribute to a survey via the ‘accepts_nested_attributes_for’ method it now becomes filtered by Rails as NOT mass-assignable.

Once I added ‘questions_attributes’ to the survey model’s ‘attr_accessible’ method and retested it worked without an issue. Hope this helps those who might be stuck trying to get nested forms to work.

Ruby: Mint12 Sqlite3 Install Issues (sqlite3.h)

On the latest version of Linux Mint 12 (the Linux platform I prefer for Ruby development) I ran a bundle install on a small application using Sqlite3 and ran into the following error message. After a little Google search I found that my installing Sqlite3 did not give Gem all the build files it requires to build the Sqlite3-ruby gem with native extensions. So if you are running into the same issue make sure you run the following commands prior to running bundle install.

<<<>>>

sudo apt-get install sqlite3
sudo apt-get install libsqlite3-dev

<<<>>>

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/home/tim/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for sqlite3.h… no
sqlite3.h is missing. Try ‘port install sqlite3 +universal’
or ‘yum install sqlite-devel’ and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

Provided configuration options:
–with-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/home/tim/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
–with-sqlite3-dir
–without-sqlite3-dir
–with-sqlite3-include
–without-sqlite3-include=${sqlite3-dir}/include
–with-sqlite3-lib
–without-sqlite3-lib=${sqlite3-dir}/lib
–enable-local
–disable-local
Gem files will remain installed in /home/tim/.rvm/gems/ruby-1.9.2-p290@CorDev1.9.2/gems/sqlite3-1.3.6 for inspection.
Results logged to /home/tim/.rvm/gems/ruby-1.9.2-p290@CorDev1.9.2/gems/sqlite3-1.3.6/ext/sqlite3/gem_make.out

Ruby: Mint12 Nokogiri Install Issues (libxml2)

If you are running into errors when installing the Ruby Gem Nokogiri and the error you are getting has to do with the library ‘libxml2’ not being available you will need to execute the following commands to fix it.

  1. sudo apt-get install build-essential
  2. sudo apt-get install libxslt-dev libxml2-dev
  3. bundle install

Original Error Message Reference:

checking for libxml/parser.h… no
—–
libxml2 is missing.  please visit http://nokogiri.org/tutorials/installing_nokogiri.html for help with installing dependencies.
—–
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

CentOS: Installing Git

You might notice that many Virtual Private Servers (VPS) are running the CentOS linux distribution. In its standard source repositories it does not have Git which we Ruby developers tend to use as our source code repositories. Due to this when you run the command ‘yum install git’ you get a message back stating it does not exist.

If you have googled for a solution you will run into a ton of really complicated solutions which require in-depth linux package management knowledge. For those of us who really aren’t interested in becoming linux server administrators its way too complicated and takes too  long to figure out. That being said I like to take the easy route and I found this through all my searching and it worked on my CentOS VPS on Hostingrails.

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm
yum install git