I'm doing a brief exercise, condensed below. The issue I'm having is that I'm able to pass a selection of all tickets, but not a selection of one ticket. At / there is no problem listing all the tickets, at endpoint for a ticket I get:
NoMethodError at /pi2l9ulnw undefined method `slug' for #
I'm relatively new to Ruby and cutting and pasting, but it seems passing @ticket to get @ticket.slug is right from the Sinatra documentation. I've gotten to this point and reached an impasse where I am trying to create a simple endpoint for 1 ticket. Any suggestions while I try to read what I'm doing, thanks.
Example below condensed to 1 file:
require 'rubygems'
require 'sinatra'
require 'haml'
require 'dm-core'
require 'dm-validations'
require 'dm-types'
require 'dm-migrations'
require 'sqlite3'
configure do
class Ticket
include DataMapper::Resource
property :id, Serial # unique key
property :slug, String # unique slug as endpoint
property :created_at, DateTime # created
property :content, Text # content of ticket
validates_uniqueness_of :slug
end
set :sessions , true
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup( :default , "sqlite3://#{Dir.pwd}/development.sqlite3" )
DataMapper.finalize
DataMapper.auto_upgrade!
end
error do
e = request.env['sinatra.error']
Kernel.puts e.backtrace.join("\n")
'Application error'
end
helpers do
def sluggenerate
rand(2**256).to_s(36)[0..8]
end
end
get '/' do
@tickets = Ticket.all(:order => [ :created_at.desc ])
haml :index
end
get '/new' do
haml :new
end
post '/new' do
p params
@ticket = Ticket.new(:slug => sluggenerate,:content => params[:content])
if @ticket.save
redirect '/'
else
redirect '/new'
end
end
get '/:slug' do
@ticket=Ticket.find(params[:slug])
haml :ticket
end
__END__
@@ layout
%html
= yield
@@ new
%h1 NEW TICKET
%form{:action => '/new', :method => 'post'}
%p
%input{:type => "text", :name => "content", :id => "content" }
%input{:type => "submit", :value => "post"}
@@ index
%div{:class => "tickets"}
- @tickets.each do |ticket|
%div{:class => "ticket"}
%h2
%a{:href => "/#{ticket.slug}" }
= ticket.slug
%div{:class => "tickettime"}
= ticket.created_at
%div{:class => "ticketcontent"}
= ticket.content
@@ ticket
%div{:class => "ticket"}
= @ticket.slug