Guest User

Untitled

a guest
Jul 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.77 KB | None | 0 0
  1. #blog.rb
  2. require 'rubygems'
  3. require 'sinatra'
  4. require 'data_mapper'
  5. #require 'Time'
  6.  
  7. #setting DB
  8. DataMapper.setup(:sqlite3, 'sqlite3//db/blog')
  9.  
  10.  
  11.  
  12. get '/articles' do
  13.     @articles = Article.all :limit => 10,
  14.                             :order=> 'created_at desc'
  15.     haml :articles
  16. end
  17.  
  18. get '/article:permalink' do
  19.     @article = Article.find :first,
  20.                             :permalink => params[:permalink]
  21.     view :article
  22. end
  23.  
  24. get '/articles/new' do
  25.     view :article_new
  26. end
  27.  
  28. post '/articles/create' do
  29.     @article = Article.new :title => params[:article_title],
  30.                            :text => params[:article_text],
  31.                            :posted_by => params[:article_posted_by],
  32.                            :permalink => create_permalink(params[:article_title])
  33.     if @article.save
  34.         redirect "/article/#{@article.permalink}"
  35.     else
  36.         redirect "/articles"
  37.     end
  38. end
  39.  
  40. get '/article/edit/:permalink' do
  41.     @article = Article.find :first,
  42.                             :permalink => params[:permalink]
  43.     view :article_edit                        
  44. end
  45.  
  46. post '/article/update/:permalink' do
  47.     @article = Article.find :first,
  48.                             :permalink => params[:permalink]
  49.     if @article
  50.         @article.title = params[:artitcle_title],
  51.         @article.text = params[:article_text],
  52.         @article.posted_by = params[:article_posted_by],
  53.         @article.updated_by = Time.now
  54.         if @article.save
  55.             redirect "/article/#{@article.permalink}"
  56.         else
  57.             redirec "/articles"
  58.         end
  59.     else
  60.         redirect "/articles"
  61.     end
  62. end
  63.  
  64.  
  65. get '/application.css' do
  66.     header 'Content-Type' => 'text/css; charset=utf8'
  67.     sass :style
  68. end
  69.  
  70. #define view helper
  71. helpers do
  72.     def view(view)
  73.         haml view
  74.         #erb view
  75.     end
  76. end
Add Comment
Please, Sign In to add comment