I’m new to rails and setting up the ticketee project based on Rails in Action 3, I’m attempting to deviate slightly and setup haml with views/projects/show.html.haml
I do have the Gemfile configured and haml installed and I tested a basic haml page and it does work.
Here is my problem, when migrating the show.html.erb to haml I am getting an error I can’t resolve.
show.html.erb:
<h1><%= @project.name %></h1>
This displays fine if I go to http://localhost:3000/projects/1 it displays the project name with id 1.
show.html.haml:
%h2= @project.name
After I replace show.html.erb with the haml and I go to the above url I get:
NoMethodError in Projects#show
Showing /ticketee/app/views/projects/show.html.haml
where line #1 raised:undefined method `name' for nil:NilClass Extracted source (around line
#1):1: %h2= @project.name Rails.root:
/ticketeeApplication Trace | Framework Trace | Full Trace
app/views/projects/show.html.haml:1:in
`_app_views_projects_show_html_haml___2329513113615295829_70311891362660'
The schema.rb definitely has the name field:
ActiveRecord::Schema.define(:version => 20120212051007) do
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
And my controller:
class ProjectsController < ApplicationController
def index
end
def new
@project = Project.new
end
def create
@project = Project.new(params[:project])
@project.save
#flash[:notice] = "Project has been created."
redirect_to @project, :notice => "Project has been created."
end
end
I assume this is just some oversight on my part as this is a very basic usage of haml.
It seems that you don’t have a show method inside of your Project controller. Since
projects/1means that it is a particular record, the show page would be called. When/projectsis called, the index method would get called.A possible show method could be:
In this method, an instance variable is assigned to
@projectwith the ID which is in the URL. This could be the answer to your problem!