Sinatra, select from Datamapper pass to Haml
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 #<Enumerable::Enumerator:0x7f04eb043b88>
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开发者_JAVA技巧.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
Changed
@@ ticket
%div{:class => "ticket"}
= @ticket.slug
to
@@ ticket
%div{:class => "ticket"}
- @ticket.each do |ticket|
%h1
= ticket.slug
and it works
Perhaps the issue is with the construction of the model and querying with DM (there is basic info on the site, but looking further for info has been scary to say the least -pages of rdoc classes and methods that don't point anywhere for the basic level I'm at)
Ticket.get(params[:slug]) gives some sort of null error -- the query pulls nothing, maybe 'slug' needs to be made into some sort of key?
The answer was mostly in the datamapper query.
Insanely helpful, discovered after the fact:
http://cheat.errtheblog.com/s/datamapper/
the end of this issue, maybe useful to someone else
get `/:slug' do
@ticket=Ticket.first(:slug=>params[:slug])
haml '%h1= @ticket.slug'
end
and variations render perfectly, so part of the issue may be improper querying with DM
精彩评论