当前位置:首页 > CN2资讯 > 正文内容

我的世界JAVA披风在哪设置

1天前CN2资讯


我的世界paperclip

(Introduction)

This is the first part of my series - covering file uploading gems available for Rails developers. For this edition, we will cover the popular Paperclip gem.

这是我系列的第一部分-涵盖可供Rails开发人员使用的文件上传gem。 在此版本中,我们将介绍流行的Paperclip宝石。

Paperclip is an easy to use file uploading tool made available by the awesome folks at thoughtbot.

回形针是一种易于使用的文件上载工具,由Thoughtbot的优秀人士提供。

In this tutorial, we will build a Rails application from scratch with file uploading functionality added. We will make use of Paperclip, seeing the awesome features it provides to us.

在本教程中,我们将从头开始构建一个Rails应用程序,并添加文件上传功能。 我们将使用Paperclip,看看它提供给我们的强大功能。

Here is a snapshot of what we want to build.

这是我们要构建的快照。





The code for this tutorial is available on Github.

Github上提供了本教程的代码。

(Setting Up)

For PaperClip to work, we need to have ImageMagick installed on our machine.

为了使PaperClip正常工作,我们需要在计算机上安装ImageMagick 。

For Mac users you can do so by running the command.

对于Mac用户,您可以通过运行命令来执行此操作。

brewinstall imagemagick

Ubuntu users should run the command.

Ubuntu用户应运行该命令。

sudo apt-get install imagemagick -y

Let us start by creating our Rails application. For the purpose of this tutorial I'll call mine ClipUploader.

让我们从创建Rails应用程序开始。 就本教程而言,我将其称为ClipUploader 。

rails new ClipUploader -T

The -T flags tells Rails to generate the new application without the default test suite.

-T标志告诉Rails生成没有默认测试套件的新应用程序。

Once that is done, you will want to add Twitter Bootstrap for the purpose of styling your application.

完成后,您将需要添加Twitter Bootstrap来设计应用程序的样式。

Open your Gemfile and add the Bootstrap gem

打开您的Gemfile并添加Bootstrap gem

#Gemfile ... gem 'bootstrap-sass'

Run the command to install the gem.

运行命令以安装gem。

bundleinstall

Rename app/assets/stylesheets/application.css to app/assets/stylesheets/application.scss and import the necessary files needed for Bootstrap to work.

将app/assets/stylesheets/application.css重命名为app/assets/stylesheets/application.scss并导入启动Bootstrap所需的必要文件。

#app/assets/stylesheets/application.scss ... @import 'bootstrap-sprockets'; @import 'bootstrap';

Using your text editor, create a new file to hold our navigation code.

使用您的文本编辑器,创建一个新文件来保存我们的导航代码。

#app/views/layouts/_navigation.html.erb <nav > <div > <!-- Brand and toggle get grouped for better mobile display --> <div > <button type="button" data-toggle="collapse" data-target="#navbar-collapse" aria-expanded="false"> <span >Toggle navigation</span> <span ></span> <span ></span> <span ></span> </button> <a href="#">ClipUploader</a> </div> <div id="navbar-collapse"> <ul > <li><%= link_to 'Home', root_path %></li> <li><%= link_to 'Upload New Image', new_photo_path %></li> </ul> </div> </div> </nav>

The code above creates a naviation bar to hold your main navigation links.

上面的代码创建一个导航栏来保存您的主要导航链接。

Tweak your application layout to look like what I have below:

调整您的应用程序布局,使其看起来像我下面的样子:

#app/views/layouts/application.html.erb <!DOCTYPE html> <html> <head> <title>ClipUploader</title> <%= csrf_meta_tags %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> </head> <body> <!-- Renders navigation file located at app/views/layouts/_navigation.html.erb --> <%= render "layouts/navigation" %> <div id="flash"> <% flash.each do |key, value| %> <div ><%= value %></div> <% end %> </div> <div > <%= yield %> </div> </body> </html>

To make the navigation bar visible in your application, you have to render it in your application layout. That is what we did above.

要使导航栏在您的应用程序中可见,您必须在应用程序布局中呈现它。 那就是我们上面所做的。

(Integrate PaperClip)

Open up your Gemfile and add the PaperClip gem.

打开您的Gemfile并添加PaperClip gem。

#Gemfile ... gem "paperclip", "~> 5.0.0"

Install the gem.

安装宝石。

bundleinstall

We will be making use of one controller, let us generate that.

我们将使用一个控制器,让我们生成它。

rails generate controller Photos

Using your text editor, edit your PhotosController to look like what I have below:

使用您的文本编辑器,编辑PhotosController使其外观如下所示:

#app/controllers/photos_controller.rb class PhotosController < ApplicationController #Index action, photos gets listed in the order at which they were created def index @photos = Photo.order('created_at') end #New action for creating a new photo def new @photo = Photo.new end #Create action ensures that submitted photo gets created if it meets the requirements def create @photo = Photo.new(photo_params) if @photo.save flash[:notice] = "Successfully added new photo!" redirect_to root_path else flash[:alert] = "Error adding new photo!" render :new end end private #Permitted parameters when creating a photo. This is used for security reasons. def photo_params params.require(:photo).permit(:title, :image) end end

In the above controller, we created three actions. The new and create actions are used when a photo is to be uploaded. If the photo gets saved the user is redirected to the root_path, else the new page is rendered.

在上面的控制器中,我们创建了三个动作。 要上传照片时,将使用new和create动作。 如果照片被保存,则用户将被重定向到root_path ,否则将呈现new页面。

Let us generate our Photo model.

让我们生成Photo模型。

rails generate model Photo title:string

Migrate your database:

迁移数据库:

rake db:migrate

We need to add a few attributes to the photos table. To do that we will make use of the generator provided by Paperclip. Run the command below from your terminal.

我们需要在photos表中添加一些属性。 为此,我们将使用Paperclip提供的生成器。 从终端运行以下命令。

rails generate paperclip photo image

This will generate a new migration file that looks like this:

这将生成一个新的迁移文件,如下所示:

#xxxxxxxx_add_attachment_image_to_photos.rb class AddAttachmentImageToPhotos < ActiveRecord::Migration def self.up change_table :photos do |t| t.attachment :image end end def self.down remove_attachment :photos, :image end end

Now run the migration.

现在运行迁移。

rake db:migrate

Open up your Photo model to add PaperClip functionality.

打开您的Photo模型以添加PaperClip功能。

#app/models/photo.rb ... #Mounts paperclip image has_attached_file :image

PaperClip cannot work if your Photo model is not equipped with it. You do so by adding the line of code used above.

如果您的Photo模型未配备,PaperClip将无法工作。 您可以通过添加上面使用的代码行来实现。

You need to create the files below and paste in the code.

您需要在下面创建文件并粘贴代码。

First, your index page should look like this.

首先,您的索引页面应如下所示。

#app/views/photos/index.html.erb <div ><h1>Upload Photo</h1></div> <div > <% @photos.each do |photo| %> <div > <!-- Renders image --> <%= link_to image_tag(photo.image.url, class: 'media-object'), photo.image.url, target: '_blank' %> </div> <div > <h4 ><%= photo.title %></h4> </div> <% end %> </div>

Your new.html.erb file should look like this.

您的new.html.erb文件应如下所示。

#app/views/photos/new.html.erb <div ><h1>Upload Photo</h1></div> <%= form_for @photo, html: { multipart: true } do |f| %> <% if @photo.errors.any? %> <div id="error_explanation"> <h2> <%= "#{pluralize(@photo.errors.count, "error")} prohibited this photo from being saved:" %> </h2> <ul> <% @photo.errors.full_messages.each do |msg| %> <li> <%= msg %> </li> <% end %> </ul> </div> <% end %> <div > <%= f.label :title %> <%= text_field :title, class: 'form-control' %> </div> <div > <%= f.label :image %> <%= f.file_field :image, class: 'form-control' %> </div> <div > <%= f.submit 'Upload Photo', class: 'btn btn-default' %> </div> <% end %>

Time to configure your routes.

是时候配置您的路线了。

Open your route configuration file and paste in the following.

打开您的路由配置文件,然后粘贴以下内容。

#config/routes.rb ... root to: "photos#index" resources :photos

We are listing the available photos on the home page of our application. Fire up your rails server and point your browser to http://localhost:3000/photos/new and you should get this.

我们在应用程序的首页上列出了可用的照片。 启动您的Rails服务器,然后将浏览器指向http://localhost:3000/photos/new ,您应该会得到它。



(Validations)

Try uploading an image and you will get an error that looks like this.

尝试上传图片,您会收到如下错误。



You get this because PaperClip is concerned about the security of your application against malicious files. To fix this open up your Photo model and add this line of code.

之所以会这样,是因为PaperClip担心应用程序对恶意文件的安全性。 要解决此问题,请打开您的Photo模型并添加以下代码行。

#app/models/photo.rb ... #This validates the type of file uploaded. According to this, only images are allowed. validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/

PaperClip provides you with validators to ensure that malicious files are not uploaded to your application - this is what you just did above. The method validates the file type. In the above code, only images are permitted to be uploaded.

PaperClip为您提供验证器,以确保不会将恶意文件上传到您的应用程序-这就是您上面所做的。 该方法验证文件类型。 在以上代码中,仅允许上传图像。

Try uploading an image again and it will work this time.

尝试再次上传图像,这次将可以正常使用。

#app/models/photo.rb ... #Use this if you do not want to validate the uploaded file type. do_not_validate_attachment_file_type :image

If you prefer to take care of the validation of files uploaded yourself. You can tell PaperClip explicitly not to validate your file type using the line of code above.

如果您愿意照顾自己上传的文件的验证。 您可以使用上面的代码行明确告诉PaperClip不要验证您的文件类型。

PaperClip provides more validation options for developers to protect their application against malicious file uploads. You have to add it to your model.

PaperClip为开发人员提供了更多的验证选项,以保护其应用程序免受恶意文件上传的侵害。 您必须将其添加到模型中。

#app/models/photo.rb ... #Validates file, file type and file size validates_attachment :image, presence: true, content_type: { content_type: "image/jpeg" }, size: { in: 0..10.kilobytes }

In the above code we want to validate that an image is uploaded, and also ensure that non-image files are not uploaded. We've also gone ahead to set the maximum size allowed to 10kilobytes.

在上面的代码中,我们要验证是否已上传图像,并确保未上传非图像文件。 我们还继续将允许的最大大小设置为10 KB。

(Deleting a Photo)

In case you are wondering if it is possible to delete a uploaded file, PaperClip has got you covered. Add the code below to your PhotosController.

如果您想知道是否可以删除上传的文件,请使用PaperClip。 将以下代码添加到您的PhotosController 。

#app/controllers/photos_controller.rb ... #Destroy action for deleting an already uploaded image def destroy @photo = Photo.find(params[:id]) if @photo.destroy flash[:notice] = "Successfully deleted photo!" redirect_to root_path else flash[:alert] = "Error deleting photo!" end end

The destroy action is called on whenever a user wants to delete a photo. You'll need to add a button on your views. So open up app/views/photos/index.html.erb and make it look like this:

每当用户想要删除照片时,都会调用destroy操作。 您需要在视图上添加一个按钮。 因此,打开app/views/photos/index.html.erb并使其如下所示:

#app/views/photos/index.html.erb <div ><h1>Photos</h1></div> <div > <% @photos.each do |photo| %> <div > <!-- Renders image --> <%= link_to image_tag(photo.image.url, class: 'media-object'), photo.image.url, target: '_blank' %> <!-- Button to delete image --> <%= link_to 'Remove', photo_path(photo), class: 'btn btn-danger', method: :delete, data: {confirm: 'Are you sure?'} %> </div> <div > <h4 ><%= photo.title %></h4> </div> <% end %> </div>

Now on your index page when you click the Remove button you will see a pop-up asking you to confirm if you want to delete the photo.

现在,在索引页面上,单击“ Remove按钮,您将看到一个弹出窗口,要求您确认是否要删除照片。



回形针VS其他宝石 ( PaperClip VS Other Gems )

You might be wondering, when is the best time for me to use PaperClip in my Rails application when compared to other gems. Here are the options I have for you.

您可能想知道,与其他宝石相比,什么时候是我在Rails应用程序中使用PaperClip的最佳时间? 这是我为您提供的选择。

  • Easy Attachment: PaperClip provides you with the option of enabling file upload in your Rails application easily. The process is involved is simple.
PaperClip is a great option when building simple application. It is advisable to avoid PaperClip is your application will be complex. For complex applications you may want to consider CarrierWave.
  • PaperClip is also not your perfect choice if your application will require multiple file upload. PaperClip handles single file upload pretty well.
  • If you need a gem that is actively maintained by an experienced team, PaperClip falls into such category. PaperClip is maintained and funded by thoughtbot. 如果您需要由经验丰富的团队积极维护的宝石,PaperClip属于此类。 PaperClip由Thoughtbot维护和资助。

结论 ( Conclusion )

We have been able to cover the basics and important aspects of file upload with PaperClip. To dig deep check out the official Github repo. In the next part of this series, we will take a look at another file uploading gem.

我们已经能够涵盖使用PaperClip上传文件的基本知识和重要方面。 要深入研究,请查看Github官方仓库 。 在本系列的下一部分中,我们将介绍另一个文件上传gem。


    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/22103.html

    分享给朋友:

    “我的世界JAVA披风在哪设置” 的相关文章

    全面了解IP测试:提升网络安全与性能的方法

    IP 测试概述 在网络技术的日常运作中,我常常接触到一个重要的概念,那就是IP测试。解剖这个词,我们可以看到它的基本含义是对IP地址进行全面的检测和验证。这不仅仅是个技术角色,同时也是我维护网络安全和稳定的重要手段。通过IP测试,我能够迅速定位网络问题,从而提高整体的网络性能,确保我们日常使用网络的...

    AS7473在网络数据传输中的重要性与应用探究

    AS7473简介 AS7473是一个重要的ASN编号,主要与网络数据传输和路由相关。它在信息技术领域中扮演着至关重要的角色,连接着不同的网络节点,确保数据能够顺利传输。想象一下,在这个数字化时代,数据的传输速度和准确性直接影响着我们的工作效率与信息交流。因此,AS7473的定义与重要性绝不容小觑。...

    VPSCheap评测:低价VPS服务的最佳选择与性能分析

    VPSCheap的概述 我第一次听说VPSCheap的时候,是在一个热闹的VPS论坛上。这个成立于2010年的主机商,主要提供KVM型VPS服务,其特点是低价格和无限流量。从那以后,我对VPSCheap的关注逐渐加深。它的数据中心位于美国达拉斯,给不少用户带来了良好的使用体验。论坛上的用户在讨论各自...

    搬瓦工补货通知及高性价比套餐推荐

    搬瓦工的补货通知对许多用户来说非常重要,尤其是在需求不断增加的背景下。补货通知不仅帮助用户了解最新的套餐信息,还能在价格优惠时把握购买机会。对于我而言,时常关注这些通知意味着能以最低的价格获得高配置的套餐,这无疑是提升我网络体验的重要一步。 为了随时获取补货信息,搬瓦工提供了多种渠道供用户选择。大家...

    DC2:动画创作、网络安全与汽车文化的多重魅力探索

    DC2 可谓是一个充满魔力的词汇,它在不同的领域中有着不同的意义。这种多样性让它成为了动画爱好者、汽车迷,甚至网络安全专家的共同话题。我对这些含义的探索,给我带来了许多启发和乐趣,让我对这个小小的组合字母有了更深刻的理解。 首先,提到 DC2,许多人可能会想到 DC2 动画软件。这款软件不仅在手机动...

    REST教程:掌握RESTful接口设计与开发最佳实践

    REST(Representational State Transfer)是一种架构风格,广泛应用于网络服务的设计。它强调通过标准的HTTP协议来实现资源的操作,设计简约而高效。在我学习RESTful接口的过程中,发现其核心特性尤其重要,包括无状态性、资源导向和统一接口等。这些特性不仅让开发变得更加...