Mais conteúdo relacionado Merb12. File upload example using rack middleware
uploader = Proc.new do |env|
request = Merb::Request.new(env)
if request.path =~ //images/upload/
params = request.params
FileUtils.mv params[:file][:tempfile].path,
Merb.root / 'public' / 'uploads' / params[:file][:filename]
headers = {quot;Content-Typequot;=>quot;text/htmlquot;,
quot;Locationquot; => quot;/imagesquot;}
[302, headers, quot;You are being redirectedquot;]
else
[404, {}, quot;No Upload here, move alongquot;]
end
end
merb = Merb::Rack::Application.new
run Rack::Cascade.new([uploader, merb])
14. Optional Path Segments
Matching
match(quot;/articles(/:year(/:month(/:day)))/:titlequot;).
to(:controller => quot;articlesquot;, :action => quot;showquot;).
name(:article)
Generating
/articles/Hello => { :title => “Hello” }
/articles/2008/07/Hello
=> { :year => “2008”, :month => “07”, :title => “Hello” }
15. Access to Request &
Params
match(quot;/secretquot;).defer_to do |request, params|
if request.session.authenticated?
params
end
end
http://www.slideshare.net/carllerche/merb-pluming-the-router-presentation
17. class Posts < Application
provides :xml
def show
@post = Post.get(params[:id])
display @post
end
end
21. class Exceptions < Merb::Controller
# handle NotFound exceptions (404)
def not_found
render :format => :html
end
# handle NotAcceptable exceptions (406)
def not_acceptable
render :format => :html
end
end
24. merb -e produciton -d -c 4
Processes
• master
• spawner
• worker
Hot Deploy
kill HUP ‘master process id’