0

我的项目中有一个表格,用于一个模型Artworks,我将其用于Artworksfields_for的资源。Photohas_many :photos

Photo模型使用回形针附加图像。

我所有的表单都是基于 AJAX 的,这意味着永远不应该重新加载页面,并且我不应该redirect_to在我的控制器中的任何地方使用(我不是)。

当我fields_for在我的表单中包含回形针 file_field 时,如果我在提交表单时创建或更新照片,我会收到以下错误:

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 %>
4

1 回答 1

1

事实证明,我遇到的问题是,您无法通过 AJAX 上传文件。

经过几天的努力,我发现了一个可以非常容易地修复它的宝石;遥控器

我必须做的全部是修复它:

  • 将 remotipart 添加到我的 gem 文件中gem 'remotipart'
  • 添加//= require jquery.remotipart到我的 app/assets/javascripts/application.js 文件
  • bundle install

那解决了它。图像现在使用“看起来像”AJAX 上传,但实际上是使用 iframe 来完成这项工作。

于 2012-10-13T03:21:54.153 回答