सिनात्रा, मोंगोडब और कैरियरवावे के एक समूह का उपयोग करते हुए, हम एक साधारण फोटो गैलरी लिखते हैं

सभी को शुभ दिन!

परिचय


इस पोस्ट को लिखने के लिए मुझे साइनट्रा के बारे में बहुत कम सामग्री और विशेष रूप से इस गुच्छा के बारे में बताया गया था। मुझे उम्मीद है कि किसी के लिए यह पोस्ट उपयोगी होगी। नीचे मैं एक सरल फोटो गैलरी बनाने के उदाहरण का उपयोग करके, एक साथ साइनट्रा , मोंगोडब और वाहकवाहक का उपयोग करने का वर्णन करूंगा । साइनट्रा और मोन्गोडब के बीच संचार के लिए हम उत्कृष्ट मणि - मोंगॉयड का उपयोग करेंगे। डिफ़ॉल्ट रूप से, यह माना जाता है कि आपके पास पहले से ही माणिक और मोंगोडब स्थापित है। चलिए शुरू करते हैं।


चरण 1


आवश्यक रत्न स्थापित करें:
 gem install sinatra haml mongoid bson_ext carrierwave carrierwave-mongoid 


चरण 2


एक पापात्रा एप्लिकेशन बनाएं:
  1. हमारे आवेदन की सूची बनाएं, इसे myapp कहें
  2. निर्देशिका के अंदर, निम्न सामग्री के साथ index.rb फ़ाइल बनाएँ:
     require 'sinatra' get '/' do 'Hello from Sinatra' end 


  3. शुभारंभ
     ruby index.rb 

  4. हम लोकलहोस्ट खोलते हैं: 4567 ब्राउज़र में, हम शिलालेख "सिनात्रा से नमस्कार" देखते हैं और आनन्दित होते हैं, मूल पापात्रा एप्लिकेशन तैयार है और काम कर रहा है


चरण 3


मोंगोडब से कनेक्शन सेट करें, और गैलरी में फोटो में जानकारी संग्रहीत करने के लिए एक डेटाबेस बनाएं। हम यह सब index.rb लिखते हैं:
 require 'sinatra' require 'haml' require 'bson' require 'mongoid' #   configure do Mongoid.configure do |config| name = "app" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end end #  Image(        SQL )   :title( ) class Image include Mongoid::Document field :title, type: String end #  get '/' do @images = Image.all haml :'index' end #   post '/' do @image = Image.new(:title => params['title']) @image.save redirect "/" end #   get '/image/:id' do @image = Image.find(params[:id]) haml :'show' end 
require 'sinatra' require 'haml' require 'bson' require 'mongoid' # configure do Mongoid.configure do |config| name = "app" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end end # Image( SQL ) :title( ) class Image include Mongoid::Document field :title, type: String end # get '/' do @images = Image.all haml :'index' end # post '/' do @image = Image.new(:title => params['title']) @image.save redirect "/" end # get '/image/:id' do @image = Image.find(params[:id]) haml :'show' end

आइए व्यू डायरेक्टरी बनाते हैं और index.haml (फोटो की एक सूची प्रदर्शित करने के लिए) और show.haml (एक फोटो प्रदर्शित करने के लिए) सामग्री के साथ जोड़ते हैं:
 #index.haml %h2 Photogallery %ul.photogallery -for image in @images %li %a{:href => "/image/#{image.id}"}=image.title %h2   %form{:name => "new_image", :id =>"new_image", :method => "POST", :enctype => "multipart/form-data"} %p %label   %br %input{:type=>"text", :name => "title"} %p %input{:type=>"submit", :value => ""} #show.haml %h1= @image.title 
#index.haml %h2 Photogallery %ul.photogallery -for image in @images %li %a{:href => "/image/#{image.id}"}=image.title %h2 %form{:name => "new_image", :id =>"new_image", :method => "POST", :enctype => "multipart/form-data"} %p %label %br %input{:type=>"text", :name => "title"} %p %input{:type=>"submit", :value => ""} #show.haml %h1= @image.title

हमने अपना एप्लिकेशन लॉन्च किया
 ruby index.rb 

हम लोकलहोस्ट पर जाते हैं: 4567 और हम फोटो जोड़ने के लिए फॉर्म के साथ एक पृष्ठ देखते हैं (अब के लिए, बस फोटो का शीर्षक), हम वहां कुछ लिखने और बचाने की कोशिश करते हैं यदि सब कुछ सहेजा गया और प्रतिबिंबित हुआ, तो हम अगले चरण पर जाते हैं, जहां हम छवियों के अपलोड को व्यवस्थित करने के लिए वाहक का उपयोग करेंगे।

चरण 4


वाहक रत्न को कनेक्ट और कॉन्फ़िगर करें, इस मणि के लिए सही ढंग से काम करने के लिए, हमें एक और मणि वाहक-मोंगॉइड स्थापित करने की आवश्यकता है, हमने पहले से ही बहुत शुरुआत में इसे स्थापित किया था। नीचे अंतिम फ़ाइल कोड है:
index.rb, index.haml, show.haml
 #index.rb require 'sinatra' require 'haml' require 'bson' require 'mongoid' require 'carrierwave' require 'carrierwave/mongoid' #   configure do Mongoid.configure do |config| name = "app" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end end # carrierwave class ImageUploader < CarrierWave::Uploader::Base storage :file end #  Image(        SQL )   :title( ) class Image include Mongoid::Document mount_uploader :image, ImageUploader, type: String field :title, type: String end #  get '/' do @images = Image.all haml :'index' end #   post '/' do @image = Image.new(:title => params['title']) @image.image = params[:image] #  @image.save redirect "/" end #   get '/image/:id' do @image = Image.find(params[:id]) haml :'show' end 
#index.rb require 'sinatra' require 'haml' require 'bson' require 'mongoid' require 'carrierwave' require 'carrierwave/mongoid' # configure do Mongoid.configure do |config| name = "app" config.master = Mongo::Connection.new.db(name) config.persist_in_safe_mode = false end end # carrierwave class ImageUploader < CarrierWave::Uploader::Base storage :file end # Image( SQL ) :title( ) class Image include Mongoid::Document mount_uploader :image, ImageUploader, type: String field :title, type: String end # get '/' do @images = Image.all haml :'index' end # post '/' do @image = Image.new(:title => params['title']) @image.image = params[:image] # @image.save redirect "/" end # get '/image/:id' do @image = Image.find(params[:id]) haml :'show' end

 #index.haml %h2 Photogallery %ul.photogallery -for img in @images %li %a{:href => "/image/#{img.id}"}= img.title %a{:href => "/image/#{img.id}"} %img{:src => img.image} %h2   %form{:name => "new_image", :id =>"new_image", :method => "POST", :enctype => "multipart/form-data"} %p %label   %br %input{:type=>"text", :name => "title"} %p %label  %br %input{:type=>"file", :name => "image"} %p %input{:type=>"submit", :value => ""} 
#index.haml %h2 Photogallery %ul.photogallery -for img in @images %li %a{:href => "/image/#{img.id}"}= img.title %a{:href => "/image/#{img.id}"} %img{:src => img.image} %h2 %form{:name => "new_image", :id =>"new_image", :method => "POST", :enctype => "multipart/form-data"} %p %label %br %input{:type=>"text", :name => "title"} %p %label %br %input{:type=>"file", :name => "image"} %p %input{:type=>"submit", :value => ""}

 #show.haml %h1= @image.title %img{:src => @image.image} 
#show.haml %h1= @image.title %img{:src => @image.image}

हम एप्लिकेशन को पुनः आरंभ करते हैं, लोकलहोस्ट पर जाएं: 4567 और हमारे आवेदन का आनंद लें, मैं छवियों का आकार बदलता हूं, संपादित करता हूं और हटाता हूं, मैं आपको अपने होमवर्क के साथ छोड़ देता हूं)

सूत्र यहाँ हैं - https://bitbucket.org/vened/imagegallery/src

Source: https://habr.com/ru/post/In139423/


All Articles