July 21, 2012
Whenever I do an operating system upgrade, I can always count on my rails applications breaking. Not only is rails "opinionated software", it is also "fragile software". Just be warned. If you are not willing to be a full time rails developer and closely follow all of the rails API changes, you can expect to be caught with your pants down.
I just did a full fresh install of Fedora 17, which is a big event for Ruby as it is the first fedora release to deliver Ruby 1.9. I have already had some trivial scripts break, so I fully expect troubles with rails.
gem install passenger yum install ruby-devel yum install httpd-devel yum install apr-devel yum install apr-util-devel passenger-install-apache2-moduleThe install of httpd-devel pulls in the two apr packages as dependencies. "APR" is Apache portable runtime, just so you know. The "passenger-install" command compiles the passenger module and then instructs me to add the following lines to my httpd.conf file:
LoadModule passenger_module /usr/local/share/gems/gems/passenger-3.0.13/ext/apache2/mod_passenger.so PassengerRoot /usr/local/share/gems/gems/passenger-3.0.13 PassengerRuby /usr/bin/rubyIn my case, these go into /etc/httpd/conf.d/passenger.conf.
After updating this file and uncommenting the RailsEnv settings in my httpd.conf file, I can do a service httpd restart without errors.
Yum would give me rails 3.0.11. The rails website tells me that I can get rails 3.2 (released in January, 2012) and they recommend ruby 1.9.3 for use with rails (this is good). They also say that rails 3.2 is the last version to support ruby 1.8 (but not 1.8.6).
su gem install railsThis is getting me 27 packages, including rails 3.2.6 -- this should be interesting. Rails 3.2.6 was released June 12, 2012 (only a month ago). Plugins are now deprecated (you should use gems for any such functionality). Rails 4.0 is on the way.
I go to my rails application, delete the Gemfile.lock and do "bundle update". After this, the game is to try to start my application, see what error pops up, figure out how to fix that problem and then repeat this process until we are happily running again.
#config.action_view.debug_rjs = true
cd /u1 mv myapp myapp_ORIG rails new myappWe are going to let the new rails build new (and compatible) scaffolding, and bit by bit transplant my old application into the new scaffold. We have done this before, and we can play with a simple demo application along the way.
Following the getting started guide at rubyonrails.org I next do:
cd myapp rake db:createAnd I get the error:
rake aborted! Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.Oh good! Another can of worms!! Some people say to install nodejs. Others say to put these two lines in my gemfile:
gem 'execjs' gem 'therubyracer', :platforms => :rubyI do this, then type "bundle install". This works!!
I could play along with the tutorial at this point, but I decide to get brave and just start transplanting my files:
copy: config/database.yml copy: config/routes.rb rename: public/index.html copy: app/models/*.rb (3 files) copy: app/controllers/display_controller.rb merge: app/controllers/application_controller.rb copy: app/helpers/display_helper.rb copy: app/views/layouts/display.rhtml mkdir: app/views/display copy: app/views/display/_form.rhtml copy: app/views/display/debug.rhtml copy: app/views/display/list.rhtml copy: app/views/display/main.rhtml copy: app/views/display/show.rhtmlThe file application_controller.rb has the line:
session :session_key => '_myapp_session_id'This may have been what was causing my problems. The idea back in the day when I set it up was to use cookies to handle the session hash, now advised against (the new scheme is to use an SQLite session table). I am still getting the error about the session method, so I comment out this line in application_controller.rb and do this:
cd myapp rake db:sessions:createThis gives me:
Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (mysql2 is not part of the bundle. Add it to Gemfile.)So I add gem 'mysql2' to my Gemfile (and get rid of the gem Sqlite3), then do bundle install and the rake db:sessions:create now works.
Now I go to config/initializers/session_store.rb and edit this file. I comment out the line that says to use cookies and uncomment the line:
Myapp::Application.config.session_store :active_record_storeAs recommended, I restart httpd.
I am advised that if I change the data type stored in the sessions I may want to do:
rake db:sessions:clear
Now I get:
Unknown action The action 'main' could not be found for DisplayControllerAnd this seems to be exactly right, I have this message commented out in my display controller, even though I have a route for it in my routes.rb ..... I uncomment an empty "main" method in display_controller.rb and now I get:
Template is missing Missing template display/main, application/main with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/u1/rails/myapp/app/views"I think I can see what is up here. I have a main.rhtml template, but I see a list of handlers that includes .erb, but not .rhtml (maybe that is old school). I rename all my .rhtml files in views/display to be .html.erb files and voila! I am on to the next issue:
Mysql2::Error: Table 'myapp_development.sessions' doesn't exist: SHOW FULL FIELDS FROM `sessions`It looks like this session business has more issues for me to solve. Whatever the rake db:sessions:create command did, it did not create a "sessions" table in the myapp_development database as it ought to have.
What I do is to go back to the file config/initializers/session_store.rb and put it back to the old cookie based scheme. But I leave commented out the offending line in Application_controller.rb that tries to specify (override maybe?) the name of the session key -- hey, I don't care what it gets called and it looks to me that you set that in the session_store.rb file now (which makes more sense anyway). This works!! Now my application is running, with some other issues mind you, but my application is the issue now, not obscure rails issues!! It is rendering only part of main.erb -- it turns out my form_tag statements used to be wrapped in <% but now must be wrapped in <%= Who woulda thought?
Now I can launch a query, but I get missing method "paginate", even after adding will_paginate to my gemfile ... restarting the web servers seems to solve this and we are on to the next issue.
Active Record is telling me:
ActiveRecord::DangerousAttributeErrorMy database has a field named "association" that active record has now decided that it owns and that I should not be using.
add the "safe_attributes" gem to my Gemfile bundle install restart the web serverAfter this, I had to change this:
text = h( mount.send(column.name) )To this:
text = mount[column.name].to_sAnd we were back on the air!
Ruby on Rails notes / [email protected]