Head First Rails is regarded as the best introductory tutorial-style book on Ruby on Rails for n00bs. Except for one major problem – it was written for Rails 2.x, rather than the current Rails 3.x. Many commands and piece of code in the book are outdated and this can lead to much frustration and wasted time for people (like me) earnestly trying to work their way through it.
I’ve created a list of modifications needed to make Head First Rails work with Rails 3.x. It started out as just my own notes but decided to make it public since nobody else seems to have done this (including, disappointingly, the publishers and authors).
The list is incomplete and may contain errors. If you can help improve it please let me know.
BTW for any Rails beginners working through Head First Rails, I strongly recommend the Rails for Zombies online tutorials. They work very well in parallel with HFR.
(If any regular reader of this blog is interested why I’m trying to learn Rails, I’m just trying to get my head around it to help me in driving our new project, YourView. More on that soon.)
I’m finding the spreadsheet invaluable as I work my way through the book. When I worked on chapter two I had to play with the stylesheet to get the images to display. I had to remove the /images/ from the path.
Thank you! Looking forward to future updates!
-Cory
Thanks Cory. I’m not sure I encountered this problem but I’ve added your suggestion to the spreadsheet.
Thanks for sharing this. It is a huge help.
Huge thanks. I was just starting to really get frustrated when I found this. I really appreciate you sharing it.
I have a problem with chapter 4 about the search option.
i have a view/client_workouts with the files: show, edit, new, find, _form(i made form because they included the forms in the ‘new’ and ‘edit’ files. i did a render action)
now he can’t find the ‘find’ file when i route to it in the webbrowser. The other files does work in the browser when i route to them.
Could someone help me?
find.html.erb:
Listing client_workouts for
Trainer
Duration mins
Date of workout
Paid amount
‘Are you sure?’, :method => :delete %>
client_workouts controller
class ClientWorkoutsController @client_workouts }
end
end
# GET /client_workouts/1
# GET /client_workouts/1.xml
def show
@client_workout = ClientWorkout.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @client_workout }
end
end
# GET /client_workouts/new
# GET /client_workouts/new.xml
def new
@client_workout = ClientWorkout.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @client_workout }
end
end
# GET /client_workouts/1/edit
def edit
@client_workout = ClientWorkout.find(params[:id])
end
# POST /client_workouts
# POST /client_workouts.xml
def create
@client_workout = ClientWorkout.new(params[:client_workout])
respond_to do |format|
if @client_workout.save
flash[:notice] = ‘ClientWorkout was successfully created.’
format.html { redirect_to(@client_workout) }
format.xml { render :xml => @client_workout, :status => :created, :location => @client_workout }
else
format.html { render :action => “new” }
format.xml { render :xml => @client_workout.errors, :status => :unprocessable_entity }
end
end
end
# PUT /client_workouts/1
# PUT /client_workouts/1.xml
def update
@client_workout = ClientWorkout.find(params[:id])
respond_to do |format|
if @client_workout.update_attributes(params[:client_workout])
flash[:notice] = ‘ClientWorkout was successfully updated.’
format.html { redirect_to(@client_workout) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @client_workout.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /client_workouts/1
# DELETE /client_workouts/1.xml
def destroy
@client_workout = ClientWorkout.find(params[:id])
@client_workout.destroy
respond_to do |format|
format.html { redirect_to(client_workouts_url) }
format.xml { head :ok }
end
end
def find
@client_workouts = ClientWorkout.find(:all,
:conditions=>[“client_name = ? OR trainer = ?”, params[:search_string], params[:search_string]])
end
end
client_workouts model
class ClientWorkout < ActiveRecord::Base
attr_accessible :client_name, :trainer, :duration_mins, :date_of_workout, :paid_amount
end
hope you can help me :)
Thanks for your great spreadsheet from Japan. This saved much time:)
In chapter 1, on page 42, “Test Drive”… I got an error when trying to add a new ticket:
“Can’t mass-assign protected attributes: phone
Rails.root: /Users/home/tickets”
The fix was to add “:phone” to the ticket.rb model file. (app/models/ticket.rb)
So that file should look like this when you’re finished:
class Ticket < ActiveRecord::Base
attr_accessible :address, :email_address, :name, :price_paid, :seat_id_seq, :phone
end
Thanks, livi17! it works
In chapter 2, I had to move the images and stylesheets to app/assets/images and app/assets/stylesheets/ directory and it worked.
In chapter 2, I had to put the images and stylesheets in app/assets/images and app/assets/stylesheets directory to make it work
In chapter 2, you also need to change the default.css file to default.css.erb and change all images to for them to work properly
Hi,
Just finished editing adding the changes I had to do to make so all the code in the book works with rails 3.2.14. Thanks to Tim for starting this.
Someone knows how to handle partials from chapter 6? What changes should I do to complete it in rails 4.x?
Thanks for all of your help!