Here’s where I am at with my code and I think I am close.
savetomongoid is a def that pushes the params array to mongoid.
I am using activesupport for the pluralize and classify methods
route :get, :delete, :post, :put, '/*/*?/?*?' do |model, action, id|
case
when request.get?
case action
when "new"
haml '#{model}/new'
when "show"
instance_variable_set('@#{model}', model.classify.find(id))
haml '#{model}/show'
when "edit"
instance_variable_set('@#{model}', model.classify.find(id))
haml '#{model}/edit'
else
instance_variable_set('@#{model.pluralize}', model.classify.asc(made))
haml '#{model}/index'
end
when request.post? || request.put?
savetomongoid(model, params[model]) ? (redirect '/#{model}/') : (redirect '/#{model}/new')
when request.delete?
model.classify.find(id).delete ? (redirect '/#{model}/') : (puts "uhh ohh")
end
end
get '/*' do
haml :silence
end
When I try to load this with any path, I get a completely blank screen, no source, however localhost:####/ brings me to my haml :silence so some of the routes are working.
the three splats should put it’s value into model, action, id. I’ve tries with something/something/something and I still get the no source page.
the pattern of splats and question marks is assumed to work via the readme on https://github.com/sinatra/sinatra
Can anyone give me a hand? I am almost certain this should work.
Also, can anyone suggest a method to check if the model exists to filter out generating crud for any arbitrary thing in model?
Your case statement looks strange.
You are testing the result of request_method, which will return the string “GET”, “PUT”, “POST”, or “DELETE” against the values of the Request’s get?, post?, or delete? methods which return true or false.
Seems like you will always fall out the case statement with no matches.
Could should be: