curl https://raw.github.com/maddox/magick-installer/master/magick-installer.sh | sh
[sudo] gem install sinatra data_mapper carrierwave carrierwave/datamapper json shotgun
# coding: utf-8 require 'sinatra' get '/' do 'REST Sinatra <a href="/posts"> </a>' end
#init.rb # coding: utf-8 require 'rubygems' require 'sinatra' require 'data_mapper' require 'carrierwave' require 'carrierwave/datamapper' require 'rmagick' require 'json'
set :public_directory, './public'
#init.rb class Post include DataMapper::Resource property :id, Serial # property :title, String #String , title "" 50 property :body, Text #Text , body "" 65535 end
#init.rb DataMapper.setup(:default, ENV['DATABASE_URL'] || 'sqlite:./db/base.db') # DataMapper.finalize # DataMapper.auto_upgrade! #
#init.rb # get '/posts' do # /posts @posts = Post.all # @posts Post erb :'index' # index.erb* end
#index.erb <strong><a href="/posts/new"> </a></strong> <h2> </h2> <% @posts.each do |post| %> <strong><a href="/posts/<%= post.id %>/edit"><%= post.title %></a></strong><br> <%= post.body %><br> <% end %>
#layout.erb <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Redactor.js + Sinatra app</title> </head> <body> <%= yield %> </body> </html>
#init.rb #Create new Post get '/posts/new' do # erb :'posts/new' # /posts/new.erb end post '/posts/new' do params.delete 'submit' @post = Post.create(params) # , Post redirect '/posts' # end
#new.erb <h3></h3> <form method="post" action="/posts/new"> <label for="title"><strong>:</strong></label><br> <input id="title" type="text" name="title" value="" style="width: 250px;"> <br> <label for="body"><strong> :</strong></label><br> <textarea id="body" name="body" style="height: 250px;"></textarea> <br> <input type="submit" name="submit" value=""> </form>
#init.rb #Edit post get '/posts/:id/edit' do # , id @post = Post.get(params[:id]) # @post id erb :'posts/edit' # /posts/edit.erb end #Update post put '/posts/:id/edit' do post = Post.get(params[:id]) post.title = (params[:title]) post.body = (params[:body]) post.save # redirect '/posts' # end #Delete post get '/posts/:id/delete' do Post.get(params[:id]).destroy # redirect '/posts' end
#edit.erb <h3></h3> <a href="/posts/<%= @post.id %>/delete"> </a><br><br> <form action="/posts/<%= @post.id %>/edit" method="post"> <input name="_method" type="hidden" value="put" /> <label for="title"><strong>:</strong></label><br> <input type="text" name="title" id="title" value="<%= @post.title %>"> <br> <label for="body"><strong> :</strong></label><br> <textarea id="body" name="body" style="height: 250px;"><%= @post.body %></textarea> <br> <input id="post_submit" name="commit" type="submit" value=""> </form>
#layout.erb <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="/js/redactor/redactor.js"></script> <link rel="stylesheet" href="/js/redactor/css/redactor.css" type="text/css">
#layout.erb <script type="text/javascript"> $(document).ready(function() { $('.redactor_1').redactor({toolbar: 'default', lang: 'ru', imageUpload: '/upload/image', imageGetJson: '/uploads/images/imageslist.json'}); }); </script>
#new.erb <h3></h3> <form method="post" action="/posts/new"> <label for="title"><strong>:</strong></label><br> <input id="title" type="text" name="title" value="" style="width: 250px;"> <br> <label for="body"><strong> :</strong></label><br> <textarea id="body" class="redactor_1" name="body" style="height: 250px;"></textarea> <br> <input type="submit" name="submit" value=""> </form>
#edit.erb <h3></h3> <a href="/posts/<%= @post.id %>/delete"> </a><br><br> <form action="/posts/<%= @post.id %>/edit" method="post"> <input name="_method" type="hidden" value="put" /> <label for="title"><strong>:</strong></label><br> <input type="text" name="title" id="title" value="<%= @post.title %>"> <br> <label for="body"><strong> :</strong></label><br> <textarea id="body" class="redactor_1" name="body" style="height: 250px;"><%= @post.body %></textarea> <br> <input id="post_submit" name="commit" type="submit" value=""> </form>
#init.rb class ImageUploader < CarrierWave::Uploader::Base def store_dir 'uploads/images' # end def extension_white_list %w(jpg jpeg gif png bmp) # jpg, jpeg, gif, png, bmp end include CarrierWave::RMagick # RMagick version :thumb do process :resize_to_fill => [100,74] # RMagick' end storage :file end
{ "thumb": "/tests/_images/1_m.jpg", "image": "/tests/_images/1.jpg" },
#init.rb class UploadedImages # include DataMapper::Resource property :id, Serial property :image, String # property :thumb, String # mount_uploader :file, ImageUploader # carrierwave end
#init.rb post '/upload/image' do params[:file] filename = params[:file][:filename] file = params[:file][:tempfile] upload = UploadedImages.new upload.file = params[:file] upload.image = params[:image] = '/uploads/images/' + File.join(filename) # upload.thumb = params[:thumb] = '/uploads/images/thumb_' + File.join(filename) # upload.save # - @images = UploadedImages.all # @images JSON File.open("public/uploads/images/imageslist.json","w") do |f| # imageslist.json f.write JSON.pretty_generate(@images) # JSON end '<img src="/uploads/images/' + File.join(filename) + '" />'# textarea end
<hh user=posts>
or <hh user=images>
just replace with @posts and images, respectively.Source: https://habr.com/ru/post/144277/
All Articles