Rails 3 Routing Extravaganza

Routing with Rails can be very confusing at first.  These are some resources that really helped me figure out what was going on – especially nested resources:

http://kconrails.com/2010/12/02/routing-in-ruby-on-rails-3/#things_you_should_know

 

https://github.com/kconrails/rails3_routing/blob/master/app/views/comments/index.html.erb

 

http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects

 

This is mostly a reference for myself to refer back to later.  But if it helps somebody else, awesome.  :)

Lynda, Rails 3 Tutorial, and Whiny Nills

Kevin Skoglund has an excellent Rails 3 tutorial on Lynda.com. However, the particular subscription that I have does not contain the code – only the videos. I found that to be an issue here, because there have been a couple of instances where some things weren’t covered in the video but were necessary to make the app work.

One of them is in Chapter 14 – Nested Pages. When you get to the part where you drill down into the Subject and then the Pages, then you try to create a Page within the corresponding subject, you will see an error: Called id for nil, which would mistakenly be 4 — if you really wanted the id of nil, use object_id.

That is a generic ‘whiny nill’ message that rails throws. It often means that you have not initialized an instance variable that is needed within the view. Or, you could have misspelled it – @Page does not equal @page for instance. However, it’s not the case here. If you use SQLite Manager to inspect the database tables, you’ll see that the page saved just fine – except that it didn’t have an associated subjec_id with it. Also, if you are using BareTail to tail the development log, you’ll notice that the subject_id is indeed being collected by the Post params but it’s not being inserted into the SQL statement.

The problem is not in the pages_controller but in the pages _form.html.erb partial.

Add this line under the “Subject” table header:

 <%= f.select(:subject_id, @subjects.collect {|s| [s.name, s.id]}) %>

This will create a collection of subject names, and their associated id’s, then this will get passed along to the Post method and the database will have an associated subject_id for each page that is created.

Undefined method `password’

If you are trying to implement authentication and getting a “undefined method ‘password’ in users/new.html.erb” or similar error message, than it may be that you need to modify your model file.

If the error is in the ‘users’ controller than  your user model – user.rb – needs to have the following code set at the top:  attr_accessor :password

After you set this, enjoy your now working authentication and get a broom to sweep up all the hair you just pulled out.

Rails and Web App Theme plugin

Brilliant tutorial here from Bryan Bibat about using the ‘web-app-theme’ gem within rails:  http://blog.bryanbibat.net/2011/01/03/starting-a-professional-rails-app-with-haml-rspec-devise-and-web-app-theme/

I had a few issues in getting it deployed to Heroku, but it’s up and running now.

On my system, I needed to include the ‘hpricot’ gem in my production environment.  Also, needed to add  ‘config.serve_static_assets = true’ in the production configuration file.

He recommends using Capistrano for deployment, but I was able to get it running on Heroku without this gem.

Rails Integration Testing on Windows with RSpec

I’m currently going through the excellent Ruby on Rails Tutorial by Michael Hartl.  If you are learing Rails for the first time – this is an invaluable resource.

However, when you get to the section on configuring Rspec for integration testing, there is a modification for the .autotest file that you will need to make and the author does not have a fix for Windows – only Mac and Ubuntu.

If you are like me and using Windows 7, though, it’s an easy fix.  The Linux version of the code should work fine.  Here’s my .autotest file:

require ‘autotest/growl’

Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(%r%^spec/(requests)/.*rb$%) do |filename, _|
filename
end
end

CodeIgniter and PHP – Video Tutorial Series

If you are new to web development and have an interest in learning a PHP framework, than this series is great:

http://net.tutsplus.com/sessions/codeigniter-from-scratch/

Jeffrey Way is an excellent instructor.  He is thinking out loud while he is coding and it really gives you insight into how you need to go about developing in an MVC environment.  If for no other reason, I would recommend watching these videos just for their wonderful insights.

I’m in the process of learning Ruby on Rails, but this series is helping me become more familiar with MVC in general.

Install Blueprint CSS into Refinery CMS

/***  Just so you know, I’m using the RailsInstaller in a Windows 7 environment.  ***/

Blueprint CSS is awesome.  Refinery CMS is awesome too.  Blueprint + Refinery is awesomer yet!

But, it’s not exactly crystal clear how to make this happen.  If you are as confuzzled as I was when I tried to do this, read on.

Once you have used the guides on Refinery’s site (http://refinerycms.com/guides) you should have your version of Refinery up and running.  Once that is done, then you need to download Blueprint – http://www.blueprintcss.org/.

When you unzip the downloaded Blueprint files, you should open it up and see a folder called – what else – Blueprint.

Simply drag and drop this folder into the public/stylesheets folder of your Refinery CMS project.

Blueprint within Refinery

Here's the Blueprint folder within Refinery

Then comes the fun part – you will need to override Refinery’s layout view.  In your terminal type:  rake refinery:override view=layouts/application.  When you look in your app/views/layouts folder, you will see the application.html.erb file that you need to edit (don’t worry about the application.html.erb.backup – Refinery did that itself in the beginning).

Inside this file  you will need to insert three stylesheet_link_tag helpers in order for Refinery to see your Blueprint stylesheets.  Right underneath <!Doctype html> put this:

<%= stylesheet_link_tag ‘blueprint/ie.css’ %>

<%= stylesheet_link_tag ‘blueprint/print.css’ %>

<%= stylesheet_link_tag ‘blueprint/screen.css’ %>

Ok, if you type rails s to run the server and then go to localhost:3000 you will see your refinery app with the Blueprint styles in it.

But… you may run into a new problem at this point.  Are you seeing the link paths in parentheses next to all of the links on your page?  If so, I got a fix for you.

The culprit is Blueprint’s print.css file.  Open that up and comment out the very last line:  a:link:after, a:visited:after {content:” (” attr(href) “)”;font-size:90%;}  

Go back and refresh your browser and your Refinery CMS site should be up and running.  Now with Blueprint!