1.安装
gem install carrierwave
2.更新 Gemfile
gem 'carrierwave'
3.生成uploader
rails generate uploader Avatar
生成文件:app/uploaders/avatar_uploader.rb
4.挂载属性
文件上传后存储的是图片的路径,存储图片路径的字段挂载AvatarUploader.比如model Restaurant有四个属性name 、author 、publish 、picture(用来存放上传图片的路径),在model中加上一行:
class Restaurant< ActiveRecord::Base
mount_uploader :picture, AvatarUploader
end
5.页面
(1),haml页面
=form_for @restaurant,:html=>{:multipart => ture},:url =>{:action=>‘save_photo’},:method =>"post" do |f|
=f.file_field : picture
=f.submit "保存"
(2)
<%= form_for @restaurant, :html => {:multipart => true} },:url =>{:action=>‘save_photo’}, do |f| %>
<label>My picture</label>
<%= f.file_field :picture%>
<%=f.submit "保存"%>
<% end %>
6,controller
def restaurant
@restaurant =Restaurant.new
end
def save_photo
picture = Restaurant.new
picture.picture= params[:restaurant][:picture]
end
相关推荐
使用 Rails 中的 Carrierwave 将图像上传到 S3。 克隆这个 repo,然后运行: bundle install rake db:migrate 之后,您需要从 AWS 获取密钥以上传到 S3。 将这些密钥存储在您的config/secrets.yml文件中。 这是...
这是一个简单的Rails应用程序,显示了如何使用CarrierWave和WebP-ffi(用Ruby编写的libwebp包装器)将图像转换为WebP格式。 笔记 如果需要在不使用CarrierWave版本的情况下转换图像,则可以使用相同的方法而无需重写...
NULL 博文链接:https://hlee.iteye.com/blog/587000
shoppe-example, 在 Rails 中使用Shoppe平台的示例存储实现 这是一个 Rails 应用程序,它使用 Shoppe 构建。 它具有一个可以爱的设计,以充分演示Shoppe平台提供的功能以及它在 Rails 应用程序中的。 正在启动要开始...
Ruby on Rails中文指南
博文链接:https://ziyoujiedao.iteye.com/blog/148307
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
NULL 博文链接:https://hlee.iteye.com/blog/345775
可实现多文件的同时上传,控制文件的格式,数量,同时兼容IE6,7,firefox,易于扩展
Ruby On Rails中文教材(PDF)
rails, Ruby on Rails 欢迎使用 RailsRails 是一个web应用程序框架,它包括根据 Model-View-Controller ( MVC ) Pattern 创建数据库备份的web应用程序所需的所有内容。理解 MVC Pattern 是理解 Rai
NULL 博文链接:https://dendrobium.iteye.com/blog/305938
敏捷Rails中文教程 敏捷Rails中文教程 敏捷Rails中文教程
ruby on rails轻松使用
rails-assets, 在 Rails 中,资产管理的解决 Rails 资产 Bundler 到 Bower 代理本自述文件涉及项目的开发方面。 访问站点了解如何在你的应用程序中使用 Rails 资产。 插件开发设置git clone git@github.com:tenex/r
CarrierWave FTP存储 该gem增加了对将FTP上传到支持安装安装最新版本: gem install carrierwave-ftp在您的代码中要求它: require 'carrierwave/storage/ftp'或者,在Rails中,您可以将其添加到您的Gemfile中: gem...
mail_form, 在 Rails 中使用 i18n 验证附件和请求信息,直接从表单发送电子邮件 MailForm Rails 3这个 gem 构建在 ActiveModel 之上,展示如何从 Rails 中提取验证。命名和 i18n,而不需要自己实现。本自述文件指的...
rails指南 中文版