Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用rails、回形针和angularjs上载多个附件_Ruby On Rails_Angularjs_Paperclip_Attachment - Fatal编程技术网

Ruby on rails 使用rails、回形针和angularjs上载多个附件

Ruby on rails 使用rails、回形针和angularjs上载多个附件,ruby-on-rails,angularjs,paperclip,attachment,Ruby On Rails,Angularjs,Paperclip,Attachment,我在这个问题上纠缠了几天,似乎遇到了麻烦。我使用的是rails 4.1、angular 1.2.23和回形针4.1。我从许多类似问题的解决方案中取样,但似乎没有一个解决了这个问题 当我在文档模型中设置回形针附件时,我可以上传一个附件。但是,当我将附件与文档模型分离,并为多个附件添加了图像模型时,我无法使其工作 以下是我正在使用的代码: doc.rb class Doc < ActiveRecord::Base has_many :image, :dependent => :d

我在这个问题上纠缠了几天,似乎遇到了麻烦。我使用的是rails 4.1、angular 1.2.23和回形针4.1。我从许多类似问题的解决方案中取样,但似乎没有一个解决了这个问题

当我在文档模型中设置回形针附件时,我可以上传一个附件。但是,当我将附件与文档模型分离,并为多个附件添加了图像模型时,我无法使其工作

以下是我正在使用的代码:

doc.rb

class Doc < ActiveRecord::Base
    has_many :image, :dependent => :destroy

    accepts_nested_attributes_for :image, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
这是我的第一个主要rails应用程序,因此非常感谢您的帮助。我还可以提供所需的任何后续信息


谢谢

你能试着上传一张/多张图片并发布params变量的内容吗?另外,你的
图片
模型应该是单数(
图片
)。当然--
由DocsController处理#创建为JSON参数:{“title”=>“Test”,“parent”=>“Chat”,“info”=>“Test”和“,“imageContent”=>“Image/png”,“imagePath”=>“屏幕截图2014-02-10:3.17.49 PM.png”,“imageData”=>“--长解码字符串和整数----”,“doc”=>{“title”=>“Test”,“parent”=>“Chat”,“info”=>“Test”和“}}}
@Josh I还将模型更新为单数
图像
。服务器响应相同。
class Image < ActiveRecord::Base
    belongs_to :doc

    has_attached_file :image, :styles => { :lrg => "700x700>", :med => "350x350>", :sml => "100x100>" }, :whiny => false,
              :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
              :url => "/system/:attachment/:id/:style/:filename" 
    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

    def image=(files = [])
      files.each{|f| (@image ||= []) << image.create(image: f) }
    end
end
class DocsController < ApplicationController
  skip_before_filter :verify_authenticity_token

  def index
    @docs = if params[:keywords] 
              Doc.where("title ilike ?", "%#{params[:keywords]}%")
            else
              []
            end

    @items = Doc.all
  end

  def show
    @doc = Doc.find(params[:id])
  end

  def create
    if params[:imageData]
      decode_image
      @doc = Doc.new(@up)
    else
      @doc = Doc.new(params.require(:doc).permit(:id, :title, :parent, :info))
    end

    if @doc.save
      render 'show', status: 201
    else
      render json: @doc.errors, status: :unprocessable_entity
      Rails.logger.info @doc.errors
    end
  end

  def update
    doc = Doc.find(params[:id])

    if params[:imageData]
      decode_image
      doc.update_attributes(@up)
    else
      doc.update_attributes(params.require(:doc).permit(:id, :title, :parent, :info))
    end  

    head :no_content
  end

  def destroy
    doc = Doc.find(params[:id])
    doc.destroy
    head :no_content
  end

  private
    def doc_params
      @up = params.require(:doc).permit(:id, :title, :parent, :info, image_attributes: [:_destroy, :id, :image])
    end

    def decode_image
      @up = params.require(:doc).permit(:id, :title, :parent, :info, image_attributes: [:_destroy, :id, :image])
      # decode base64 string
      Rails.logger.info 'decoding now'
      decoded_data = Base64.decode64(params[:imageData]) # json parameter set in directive scope
      # create 'file' understandable by Paperclip
      @data = StringIO.new(decoded_data)
      @data.class_eval do
        attr_accessor :content_type, :original_filename
      end

      # set file properties
      @data.content_type = params[:imageContent] # json parameter set in directive scope
      @data.original_filename = params[:imagePath] # json parameter set in directive scope

      # update hash, I had to set @up to persist the hash so I can pass it for saving
      # since set_params returns a new hash everytime it is called (and must be used to explicitly list which params are allowed otherwise it throws an exception)
      @up[image_attributes: [:image]] = @data # image is the model attribute that is defined as an attachment using paperclip generator
    end
end
angular.module('fileUpload', []) // using restangular is optional

.directive('uploadImage', function () {
return {
 restrict: 'A',
 link: function (scope, elem, attrs) {
  // listens on change event
  elem.on('change', function() {
    console.log('entered change function');
    for (var i = 0; i < elem.length; i++) {
      for (var x = 0; x < elem[i].files.length; x++) { 
        var file = elem[i].files[x];
        // gathers file data (filename and type) to send in json
        scope.doc.imageContent = file.type;
        scope.doc.imagePath = file.name;
        console.log(scope.doc.imagePath);
       // converts file to binary string
        var reader = new FileReader();
        reader.readAsBinaryString(file);
        reader.onload = function (e) {
        // retrieves the image data from the reader.readAsBinaryString method and stores as data
        // calls the uploadImage method, which does a post or put request to server
        scope.doc.imageData = btoa(e.target.result);
        // updates scope
      }
      scope.uploadImage(scope.doc.imagePath);
      scope.$apply();
    }
    };
  });
 },
 controller: ['$scope', '$location', '$resource', function($scope, $location, $resource){

  $scope.uploadImage = function (path) {
    var Doc;
    Doc = $resource('/docs/:docId', {
      docId: "@id",
      format: 'json'
    }, {
      'save': {
        method: 'PUT'
      },
      'create': {
        method: 'POST'
      }
    });
   // if updating doc
    console.log($scope.doc);
    var onError;
    onError = function(_httpResponse) {
      return flash.error = "Something went wrong";
    };

    if ($scope.doc.id) {
      $scope.doc.$save((function() {
        $scope.docImageLink = baseUrl + $scope.doc.image_url;
      }), onError);
    } else {
        console.log("create");
    }
   };
 }]
};
});
<input type="file" id="doc.image" name="doc.image" ng-model='doc.image' upload-image multiple/>
<img ng-src="{{userImageLink}}" ng-click="openFileWindow()" ng-class="{ hidden: !userImageLink}" >
<div class="drop-box" ng-click="openFileWindow()" ng-class=" {hidden: userImageLink}">  
  Add Image
</div>
ActiveRecord::UnknownAttributeError (unknown attribute: {:image_attributes=>[:image]}):
app/controllers/docs_controller.rb:21:in `create'