rails mysql mongoid carrierware gridfs 实现图片上传功能

1.安装

Git: git的安装
MongoDB: MongoDB安装
Nginx: Nginx安装
注: 官方的nginx没有nginx-gridfs功能,需要手动编译进去

安装步骤:
[bash]
cd /tmp
git clone https://github.com/mdirolf/nginx-gridfs
cd nginx-gridfs/
git submodule init
git submodule update
#在 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 上编译时,可能会遇到问题。可以在configure时加上
./configure –add-module=/path/to/nginx-gridfs/source/ –with-cc-opt=-Wno-missing-field-initializers
make && make install

#编辑nginx配置文件
vim /usr/local/nginx/conf/nginx.conf

#注: gridfs 指路由名
location /gridfs/ {
gridfs my_app
root_collection=pics
field=_id
type=int
user=foo
pass=bar;
mongo 127.0.0.1:27017;
}
[/bash]
参考: https://github.com/mdirolf/nginx-gridfs

http://serverfault.com/questions/345516/compiling-nginx-with-nginx-gridfs-getting-mongo-c-driver-errors-during-make

二.rails配置

1,搭建rails环境 -> rvm 参考:rails环境搭建
[bash]
vim Gemfile
source ‘http://rubygems.org’
gem ‘rails’
gem ‘mysql2′
gem ‘carrierwave’
gem ‘mini_magick’
gem ‘mongo’
gem ‘bson’
gem ‘bson_ext’

bundle install
[/bash]

2,创建一个rails项目
[bash]
rails new product –database=mysql
rails g scaffold photo name:string image:string #image存储图片名
[/bash]

3,通过generator生成uploader 文件在app/uploader/base_uploader.rb
[bash]
rails g uploader base
#以上创建的父类文件,可以在app/uploader下创建子类文件用于不同的图片上传需要
#修改base_uploader.rb文件并加入MiniMagic,用来对图片进行处理,改变大小等。
# encoding: utf-8
class BaseUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :grid_fs
end

vim app/uploaders/photo_uploader.rb
# encoding: utf-8
class PhotoUploader < BaseUploader
process :resize_to_limit => [800, 800]
version :thumb do
process :resize_to_limit => [30, 30]
end
end
[/bash]

4, 在model中 mount uploader到image 字段中
[bash]
class Photo < ActiveRecord::Base
attr_accessible :image, :name
mount_uploader :image, PhotoUploader
end
[/bash]

5.在config/initializers/下创建文件carrierwave.rb配置mongodb
[bash]
vim config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.grid_fs_database = ‘photos’ #数据库名
config.grid_fs_host = ‘localhost’ #mongodb地址
config.grid_fs_access_url = "/uploader" #好像是访问时图片url的前缀
end
[/bash]

6.html模板部分
[bash]
#form
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
#index
<%= image_tag photo.image %>
<%= image_tag photo.image_url(:thumb) %>
[/bash]

7. 启动部分
如果是在develop模式下,需要配置路由已经创建controller文件
[bash]
vim app/controller/gridfs_controller.rb

class GridfsController < ActionController::Metal
def serve
gridfs_path = env["PATH_INFO"].gsub("/upload/", "")
begin
gridfs_file = Mongo::GridFileSystem.new(Mongo::DB.new(‘photos’, Mongo::Connection.new(‘localhost’))).open(gridfs_path, ‘r’)
self.response_body = gridfs_file.read
self.content_type = gridfs_file.content_type
rescue Exception => e
self.status = :file_not_found
self.content_type = ‘text/plain’
self.response_body = ”
raise e
end
end
end

vim config/routes.rb
if Rails.env.development?
match "/upload/*path" => "gridfs#serve"
end
[/bash]

8.配置nginx
[bash]
location /images/ {
gridfs photos root_collection=fs field=filename type=string;
mongo 127.0.0.1:27017;
}
[/bash]

9. 安装 nginx-gridfs 模块

注意事项: 如果需要使用nginx直接读取mongo gridfs, 需要重新编辑nginx 安装nginx-gridfs模块
官方的nginx没有nginx-gridfs功能,因此需要手动编译进去。
安装步骤见项目的github主页: https://github.com/mdirolf/nginx-gridfs

遇到的问题及解决方法:
[bash]
# 在 gcc 版本 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC) 上编译时,可能会遇到问题。可以在configure时加上
–with-cc-opt=-Wno-missing-field-initializers

# 选项试试。( 见 http://serverfault.com/questions/345516/compiling-nginx-with-nginx-gridfs-getting-mongo-c-driver-errors-during-make )

# 当make是出现错误
#错误: 变量‘chunksize’被设定但未被使用 [-Werror=unused-but-set-variable]
#cc1: all warnings being treated as errors
#去掉objs/makefile中 -Werror这样一段,重新make就ok了
[/bash]

内容概要:本文详细介绍了基于FPGA的144输出通道可切换电压源系统的设计与实现,涵盖系统总体架构、FPGA硬件设计、上位机软件设计以及系统集成方案。系统由上位机控制软件(PC端)、FPGA控制核心和高压输出模块(144通道)三部分组成。FPGA硬件设计部分详细描述了Verilog代码实现,包括PWM生成模块、UART通信模块和温度监控模块。硬件设计说明中提及了FPGA选型、PWM生成方式、通信接口、高压输出模块和保护电路的设计要点。上位机软件采用Python编写,实现了设备连接、命令发送、序列控制等功能,并提供了一个图形用户界面(GUI)用于方便的操作和配置。 适合人群:具备一定硬件设计和编程基础的电子工程师、FPGA开发者及科研人员。 使用场景及目标:①适用于需要精确控制多通道电压输出的实验环境或工业应用场景;②帮助用户理解和掌握FPGA在复杂控制系统中的应用,包括PWM控制、UART通信及多通道信号处理;③为研究人员提供一个可扩展的平台,用于测试和验证不同的电压源控制算法和策略。 阅读建议:由于涉及硬件和软件两方面的内容,建议读者先熟悉FPGA基础知识和Verilog语言,同时具备一定的Python编程经验。在阅读过程中,应结合硬件电路图和代码注释,逐步理解系统的各个组成部分及其相互关系。此外,实际动手搭建和调试该系统将有助于加深对整个设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值