我的项目中有一个模型Artworks
的表单,我在其中使用fields_for
作为资源Photo
艺术品has_many :photos
。
Photo
模型使用回形针来附加图像。
我所有的表单都是基于 AJAX 的,这意味着页面永远不应该重新加载,我不应该在我的控制器中的任何位置使用redirect_to
(我不是)。
当我在回形针file_field中包含表单中的fields_for
时,如果我在提交表单时创建或更新照片,则会收到以下错误:
Template is missing
Missing template artworks/show, application/show with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/david/rails/piml_artwork/app/views" * "/home/david/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/app/views"
销毁映像复选框按预期运行。更新表单上的其余详细信息并按预期提交,只需返回 AJAX 响应并填写页面即可。
我的作品控制器如下:
class ArtworksController < ApplicationController
before_filter :load
authorize_resource
respond_to :html, :json, :js
def load
end
def show
@artwork = Artwork.find(params[:id])
end
def index
@artworks = Artwork.all
end
def new
@artwork = Artwork.new
@artwork.photos.build unless @artwork.photos.count > 0
end
def create
@artwork = Artwork.new(params[:artwork])
if @artwork.save
flash[:notice] = "Artwork saved"
@artworks = Artwork.all
respond_with @artwork
end
end
def edit
@artwork = Artwork.find(params[:id])
@artwork.photos.build unless @artwork.photos.count > 0
end
def update
@artwork = Artwork.find(params[:id])
if @artwork.update_attributes(params[:artwork])
flash[:notice] = "Artwork updated"
@artworks = Artwork.all
respond_with @artwork
end
end
def destroy
@artwork = Artwork.find(params[:id])
@artwork.destroy
flash[:notice] = "Artwork destroyed"
@artworks = Artwork.all
end
end
引起冲突的形式是这样的:
<%= form_for( @artwork, remote: true, mutlipart: true ) do |f| %>
<%= render 'layouts/error_messages', object: f.object %>
<div id="artwork-title">TITLE: <%= f.text_field :title %></div>
<div id="artwork-title-underline"></div>
<div id="artwork-show-picture"><%= image_tag @artwork.photos.first.photo.url(:medium) unless @artwork.photos.first.new_record? %>
<div id="artwork-picture-upload">
<%= f.fields_for :photos, remote: true, multipart: true do |p| %>
<% if p.object.new_record? %>
UPLOAD PHOTO
<%= p.file_field :photo, value: 'Upload Image' %>
<% else %>
REPLACE PHOTO
<%= p.file_field :photo, value: 'Replace Image' %>
<br />
<br />
<%= p.check_box :_destroy %>
<%= p.label :_destroy, 'Delete Image' %>
<% end %>
<% end %>
</div>
</div>
<div id="artwork-public">
ARTIST<br />
<%= f.select :artist_id, Artist.select_list, include_blank: "SELECT ARTIST" %><br />
<br />
DATE OF WORK <br />
<%= f.text_field :date_of_work %><br />
<br />
<div id="artwork-modify-buttons">
<%= f.submit %>
</div>
<% end %>
我的 JS 更新文件是:
<% if !@artwork.errors.any? %>
$("#facebox .content").html("<%= escape_javascript(render(partial: "/artworks/show")) %>");
$("#artworks-col").html(" <%= escape_javascript(render(partial: "/admin/artworks_list" )) %>")
<% else %>
$("#facebox .content").html("<%= escape_javascript(render(partial: "/artworks/form")) %>");
<% end %>
事实证明,我遇到的问题是,根据设计,您无法通过 AJAX 上传文件。
经过几天的辛苦,我发现了一颗宝石,可以非常轻松地修复这一切;远程处理
我必须做的修复它的全部工作是:
- 将远程处理添加到我的 gem 文件
gem 'remotipart'
- 将
//= require jquery.remotipart
添加到我的应用程序/资产/javascripts/应用程序.js文件 -
bundle install
这解决了它。图像现在使用"看起来像"AJAX上传,但实际上是使用iframe来完成工作。