ruby-on-rails - Rails4通过输入上传完成后如何触发引导模式?
问题描述
我的宝石文件:
gem 'rails', '4.2.8'
gem 'carrierwave', '~> 1.2', '>= 1.2.2'
gem 'mini_magick', '~> 4.8'
gem 'Jcrop', '~> 0.1.0'
现在我想使用form_for
上传用户图片,我的show.html.erb
:
<%= form_for @user, :html => {:multipart => true} do |f| %>
<div class="row " id="user_avatar_crop">
<!-- Choose Image -->
<div class="col-md-12">
<%= f.file_field :picture, id: :user_avatar, onchange: 'this.form.submit()'%>
</div>
</div>
<% end %>
<!-- Modal -->
<div id="uploadModalContent">
</div>
<!-- Show user picture -->
<% if @user.picture? %>
<%= image_tag @user.picture.url(:thumb), :alt => @user.name+"_avatar" %>
<% else %>
<%= image_tag @user.picture.url(:thumb),:alt => @user.name+"_default" %>
<% end %>
我的user_controller.rb
:
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
if params[:user][:picture].present?
respond_to do |format|
format.html do
flash[:warning] = "Template missing"
redirect_to @user
end
format.js { render template: 'users/update.js.erb'}
end
else
redirect_to @user
flash[:success] = "Success update"
end
else
render :edit
end
end
我的update.js.erb
:
$('#uploadModalContent').html("<%= j render "users/modal"%>");
$('#upload-modal').modal('show');
我的_modal.html.erb
:
<div class="modal fade" id="upload-modal" aria-labelledby="modalLabel" role="dialog" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Crop Image</h4>
</div>
<div class="modal-body">
<div class="col-md-8">
<%= image_tag @user.picture_url(:large), id: "cropImage" %>
</div>
<div class="col-md-4">
<h4>Preview</h4>
<div style="width:100px; height:100px; overflow:hidden;">
<%= image_tag @user.picture.url(:large), :id => "user_preview" %>
</div>
</div>
</div>
<div class="modal-footer">
<%= form_for @user do |f| %>
<% %w[x y w h].each do |attribute| %>
<%= f.hidden_field "crop_#{attribute}" %>
<% end %>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<%= f.submit "crop" %>
<% end %>
</div>
</div>
</div>
</div>
现在我需要上传图片后,_modal.html.erb
才能显示。但似乎format.js { render template: 'users/update.js.erb'}
in无法user_controller.rb
正常工作。这是为什么呢?
完成user_controller.rb
后我应该怎么做才能渲染到模态窗口?非常感谢你的帮助。input
onchange: 'this.form.submit()'
解决方案
我找到了另一种在 Rails 中上传图像的方法。我得出结论,这是迄今为止我所知道的最好的方法。您必须使用carrierwave gem。我现在将放置使用它所需的代码。无论如何,如果您可以查看 github repo 或这篇文章。
好的,让我们走吧。您必须首先在全球范围内安装 gem,甚至在您的项目中本地安装。
$ gem install carrierwave
在 Rails 中,将其添加到您的 Gemfile:
gem 'carrierwave', '~> 1.0'
现在重新启动服务器以应用更改。
首先生成一个上传器:
rails generate uploader Photos
这应该给你一个文件:
# app/uploaders/photos_uploader.rb
class PhotosUploader < CarrierWave::Uploader::Base
storage :file
# will save photos in /app/public/uploads
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
创建照片迁移
class CreatePhotos < ActiveRecord::Migration
def change
create_table :photos do |t|
t.string :name, :null => false
t.binary :data, :null => false
t.string :filename
t.string :mime_type
t.timestamps null: false
end
end
end
和型号
require 'carrierwave/orm/activerecord'
class Photo < ActiveRecord::Base
mount_uploader :data, PhotosUploader
end
然后控制器
class PhotosController < ApplicationController
def index
@photos = Photo.all
end
def show
@photo = Photo.find(params[:id])
end
def new
@photo = Photo.new
end
def create
# build a photo and pass it into a block to set other attributes
@photo = Photo.new(photo_params)
# normal save
if @photo.save
redirect_to(@photo, :notice => 'Photo was successfully created.')
else
render :action => "new"
end
end
private
def photo_params
params.require(:photo).permit!
end
end
表格上传:
<!-- new.html.erb -->
<%= form_for(@photo, :html => {:multipart => true}) do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :data %>
<%= f.file_field :data %>
</div>
<div class="actions">
<%= f.submit "Upload" %>
</div>
<% end %>
然后您将文件加载到这样的视图中。
<!-- show.html.erb -->
<h3>Photo: <b><%= @photo.name %></b></h3>
<%= image_tag @photo.data.url %>
你也可以像这样在上传图片后触发模式:
# app/assets/javascripts/photos.coffee
$ ->
alert('Photo Uploaded - You can launch modal here')
好吧,就是这样。让我知道进展如何!
推荐阅读
- javascript - 在firefox上使用ajax从服务器下载文件
- c - 如果 gcc -O3 没有“volatile”,为什么一个简单的 C“hello world”程序不起作用?
- powershell - 使用 PowerShell 在相邻 OU 中查找 Active Directory 用户对象以内置用户 OU
- javascript - Vue:使用道具而不是引用父数据的原因?
- cassandra - Cassandra 令牌或哈希值
- android - Flutter 中的有效手势渲染
- jenkins - 在詹金斯脚本管道中找不到 mvn 命令
- python - 在大 txt 文件 pandas 中查找标题
- c# - 如何使用 Document DB 客户端查询 Cosmos DB
- node.js - 在 childprocess.exec 错误上执行函数