我可以在回形针模型上放置多个图像

时间:2015-06-12 05:28:33

标签: ruby-on-rails paperclip

我可以在回形针模型上放置多个图像吗?我应该怎么说呢?

rails g migration add_image_to_model_name

    def self.down
        remove_attachment :model_name, :image, :image2
      end

angular.module('todo', [])

.controller('ToDoCtrl', ['$scope', '$window', function($scope, $http, $window) {
    $scope.grocerylist = $window.localStorage.getItem('grocerylist') || [];
    $scope.priority = 'normal';

    $scope.addTask = function(event) {
        if(event.keyCode == 13 && $scope.groceryName.length) {
            $scope.grocerylist.push({"name": $scope.groceryName, "completed": false});   
            $scope.groceryName = "";
            $window.localStorage.setItem('grocerylist', $scope.grocerylist);
        }
    }

    $scope.deleteGrocery = function(index) {
        $scope.grocerylist.splice(index, 1);
        $window.localStorage.setItem('grocerylist', $scope.grocerylist);
    }
}])

end

1 个答案:

答案 0 :(得分:2)

最好的方法是为回形针附件创建新表,并在此表与您的父/现有表之间建立has-many关联。

通过这种方法,您可以根据需要上传图像。

# app/models/gallery.rb
class Gallery < ActiveRecord::Base
  has_many :pictures, :dependent => :destroy
end

创建picture模型和迁移文件,然后在has_attached_file模型中定义回形针picture

# app/models/picture.rb
class Picture < ActiveRecord::Base
  belongs_to :gallery

  has_attached_file :image,
    :path => ":rails_root/public/images/:id/:filename",
    :url  => "/images/:id/:filename"
end

以下是您可以参考的教程:Adding multiple images to a Rails model with paperclip