سؤال

I have a problem with the rmagick.

updater>assets_uploader

   include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  # include Sprockets::Helpers::RailsHelper
  # include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
   def scale(width, height)
     # do something
   end

  # Create different versions of your uploaded files:
   version :thumb do
     process :scale => [50, 50]
   end

controller>predictions

def new
        @prediction = Prediction.new
        2.times { @prediction.assets.build }
    end

def create
        @prediction = Prediction.new(prediction_params)
        if @prediction.save
        flash[:notice] = "Prediction has been created."
        redirect_to @prediction
        else
        flash[:alert] = "Prediction has not been created."
        render 'new'
        end
    end

private
    def prediction_params
        params.require(:prediction).permit(:match,:pick, :odds,:bookmaker,:result,assets_attributes: [:asset])
    end

models>prediction

class Prediction < ActiveRecord::Base
    validates :match, presence: true


    has_many :assets
    accepts_nested_attributes_for :assets
end

model>assets

class Asset < ActiveRecord::Base
     belongs_to :prediction

        mount_uploader :asset, AssetUploader

end

dbs

i have a table

class CreateAssets < ActiveRecord::Migration
  def change
    create_table :assets do |t|
        t.string :asset
        t.references :prediction

      t.timestamps
    end
  end
end

class AddAssetToPredictions < ActiveRecord::Migration
  def change
    add_column :predictions, :asset, :string
  end
end

prediction table:

class CreatePredictions < ActiveRecord::Migration
  def change
    create_table :predictions do |t|
      t.string :match
      t.string :pick
      t.string :bookmaker
      t.string :odds
      t.string :result

      t.timestamps
    end
  end
end

I upload 2 photos. in the show or index only the one image is 50,50 the other doesnt change.Any suggestions how to fix this?

هل كانت مفيدة؟

المحلول

You shouldn't need to perform an assets.build for upload more than one photo.

The best way to set up an asset library for your application is to start by specifying a bespoke model for your assets, i.e. attachments. You can then utilise the polymorphic ActiveRecord association (http://railscasts.com/episodes/154-polymorphic-association) to share the Attachment model across multiple models. I would also recommend using Carrierwave as your choice of file management.

Here, in this example, I have set up a polymorphic association called 'attachable':

attachment.rb

def Attachment << ActiveRecord::Base
   attr_accessible :attachable_id, :attachable_type, :description, :file
   belongs_to :attachable, polymorphic: true
   mount_uploader :file, AssetUploader
end

I have also assigned two new attributes to the attachment model which will contain the associated model ID (in your case the prediction ID) and the attached type. Also, notice I have assigned the mount_uploader from Carrierwave to the attachment table (see carrierwave documentation for more information on this).

prediction.rb

def Prediction << ActiveRecord::Base
   attr_accessible :title, :description, :attachments_attributes
   has_many :attachments, as: :attachable, :dependent => :destroy
   accepts_nested_attributes :attachments
end

Now, within my Prediction model, I have created a has_many relation called attachments, which references the attachable polymorphic association in the Attachment table. Furthermore, the associated attachments are dependent on the prediction and will be removed if the prediction is removed.

I have also set up the Attachment model to be editable within the Prediction model. Therefore, you can assign nested fields within the Prediction forms.

Our final step is to configure the Carrierwave uploader file. I'm using MiniMagick Gem to compress and resize the images into different versions, while utilising the 'from_version' method in order to improve processing speed. See below:

app/uploaders/asset_uploader.rb

class AssetUploader < CarrierWave::Uploader::Base
  include CarrierWave::Compatibility::Paperclip
  include CarrierWave::MiniMagick
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  process resize_to_fit: [500,500]

  version :large do
    process resize_to_fill: [400,400]
  end

  version :medium, :from_version => :large do 
    process resize_to_fill: [150,150]
  end

  version :small, :from_version => :medium do
    process resize_to_fill: [50,50]
  end

end

This should be enough to get you started.

UPDATE FOR COMMENTS

My previous comment was to demonstrate best practise for handling the upload of assets, as it allows for scalability later on.

As for resizing images, I have always used MiniMagick as it has always been quick and easy to set up with very few problems. I haven't had much luck with RMagick in the past. Is there a specific reason you want to use RMagick?

UPDATE FOR COMMENTS #2

You can set the list of allowed file types inside your asset_uploader.rb:

def extension_white_list
    %w(jpg jpeg gif png)
end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top