user00000123
user00000123

Reputation: 1

RefineryCMS - Custom Image Engine using Dragonfly for Multiple Resize on upload - MassAssign Error when trying commit uid to database

I've learnt much from the invaluable resource stackoverflow is. I am exceedingly greatful for the many excellent contributions made. Long live open source and those that make it real!

For all the great information here already, I have not been able to find the right approach to my issue here. Let me explain.

Using RefineryCMS v. 2.0.9 I have generated an engine within a rails app for presentation of 'Portfolios'. A 'Portfolio' has_many ':exhibits'. ':exhibit' belongs_to ':portfolio' as per models below.

It is intended that model attribute :img_orginal - contains the uid for the Dragonfly image upload. This works as it should, image upload uid is commited to database and can be called to views/portfolios/show.html.erb view as intended. It is intended the site be responsive. If it is to be responsive, it needs a suite of resized thumbs generated from the original. These need to be generated by Dragonfly when :img_original is uploaded and commited via the backend form. So far I have been unable to achieve this using the Dragonfly image_accessor within the exhibits model. I receive the followin exception upon creation attempt.

Can't mass-assign protected attributes: img_original ActiveModel::MassAssignmentSecurity::Error in Refinery::Portfolios::Admin::ExhibitsController#create

Both databases exist and are linked correctly via ':portfolio_id' foreign key in the 'exhibits' model. All model attributes are whitelisted as per current rails best practice.

What am I missing? The controller methods aren't right I assume, I cannot figure out how they should be. Is it necessary to ammend the engine controller to call in an instance the Page Image class, or Dragonfly itself? Do I need to change routes.rb. Could someone advise how I'd define the Controller's create and update methods. Is it even necessary, given they should already be defined by page_images controller of the main refinery app.

I have experimented with methods suggested in the Dragonfly documentation using image_accessor to no avail. Here are some of the approaches taken thus far.

ie.

image_accessor :img_original do
after_assign :crop_thumb     #method defined below
            #:resize_mobile,
            #:resize_tablet,
            #:resize_desktop
# OR
#  copy_to(:img_thumb) do
#    |a| a.thumb('100x100')
#    end
# OR different block syntax
#  copy_to(:img_thumb) do {|a| a.thumb('100x100')}
end

def crop_thumb
  @exhibit.img_thumb = @exhibit.img_original.process(:resize_and_crop,
                                                     :width => 100,
                                                     :height=> 100,
                                                     :x => :x_focus,
                                                     :y => :y_focus).encode(:jpg, '-quality 60').apply
end

#def resize_mobile,
   @exhibit.img_mobile = @exhibit.img_original.process(etc.
#end

#def resize_tablet,
   etc.

I'm very new to this, I've persisted thus far researching all I can to achieve it. And yet I come to this and cannot crack it. I would appreciate any assistance one might offer. Thank you.

Code examples and environment info below.

Environment; Ubuntu 12.10 RubyMine IDE Ruby 1.9.3 RVM to handle gems Rails 3.2.9 SQLite dev database RefineryCMS (including page_images and inquiries plugins) Dragonfly 0.9.14

Portfolio Engine generated with these migrations

 # This migration comes from refinery_portfolios (originally 1)
 class CreatePortfoliosPortfolios < ActiveRecord::Migration

 def up
 create_table :refinery_portfolios do |t|
  t.string :title
  t.integer :year
  t.text :description
  t.integer :publication_id
  t.integer :commentary_id
  t.integer :position
  t.timestamps
end
end

def down
if defined?(::Refinery::UserPlugin)
  ::Refinery::UserPlugin.destroy_all({:name => "refinerycms-portfolios"})
end

if defined?(::Refinery::Page)
  ::Refinery::Page.delete_all({:link_url => "/portfolios/portfolios"})
end

drop_table :refinery_portfolios
end
end

And

# This migration comes from refinery_portfolios (originally 2)
class CreatePortfoliosExhibits < ActiveRecord::Migration

def up
create_table :refinery_portfolios_exhibits do |t|
  t.string :title
  t.integer :cat_num
  t.integer :year
  t.string :medium
  t.integer :width
  t.integer :height
  t.integer :img_original_id
  t.string :img_thumb
  t.string :img_mobile
  t.string :img_tablet
  t.string :img_desktop
  t.integer :x_focus
  t.integer :y_focus
  t.text :history
  t.integer :portfolio_id
  t.boolean :sold
  t.integer :position

  t.timestamps
  end

  end

def down
 if defined?(::Refinery::UserPlugin)
  ::Refinery::UserPlugin.destroy_all({:name => "refinerycms-portfolios"})
end

if defined?(::Refinery::Page)
  ::Refinery::Page.delete_all({:link_url => "/portfolios/exhibits"})
end

drop_table :refinery_portfolios_exhibits

end

end

And refinery_portfolios_exhibits table column headers changed

 Exhibits table column header changed via migration
 class FixColumnName < ActiveRecord::Migration
  def change
   rename_column :refinery_portfolios_exhibits, :img_original_id, :img_original_uid
   rename_column :refinery_portfolios_exhibits, :img_thumb, :img_thumb_uid
   rename_column :refinery_portfolios_exhibits, :img_mobile, :img_mobile_uid
   rename_column :refinery_portfolios_exhibits, :img_tablet, :img_tablet_uid
   rename_column :refinery_portfolios_exhibits, :img_desktop, :img_desktop_uid
  end
 end

Portfolio Model contains portfolio metadata, called 'portfolio.rb' has_many exhibits module Refinery module Portfolios class Portfolio < Refinery::Core::BaseModel self.table_name = 'refinery_portfolios'

  attr_accessible :title,
                  :year,
                  :description,
                  :publication_id,
                  :commentary_id,
                  :position

  acts_as_indexed :fields => [:title]

  validates :title, :presence => true, :uniqueness => true

  belongs_to :publication, :class_name => '::Refinery::Resource'
  belongs_to :commentary, :class_name => '::Refinery::Resource'

  # destroy all exhibits when a portfolio is destroyed
  has_many :exhibits, :dependent => :destroy

end
end
end

Exhibit Model contains image link and associated metadata, called 'exhibit.rb'; belongs_to portfolio

require 'dragonfly/rails/images'

module Refinery
module Portfolios
class Exhibit < Refinery::Core::BaseModel

attr_accessible   :title,
                  :cat_num,
                  :year,
                  :medium,
                  :width,
                  :height,
                  :img_original_uid,   #image upload uid from refinery_images table
                  :img_thumb_uid,      #commit resize uid here
                  :img_mobile_uid,     #here
                  :img_tablet_uid,     #here
                  :img_desktop_uid,    #and here
                  :x_focus,
                  :y_focus,
                  :history,
                  :portfolio_id,   #foreign key, associates exhbit with portfolio
                  :sold,
                  :position

  acts_as_indexed :fields => [:title, :medium, :year, :cat_num]

  validates :title, :presence => true, :uniqueness => true
  validates :year, :length => { :is => 4,
                                :message => "Four digit year format,(yyyy) eg. 2013"}
  validates :img_original_uid, :presence => true, :uniqueness => true                             
  validates :portfolio_id, :presence => true
  validates_associated :portfolio
  validates :x_focus, :presence => true
  validates :y_focus, :presence => true
  validates :sold, :inclusion => {:in => [true, false]}

  belongs_to :img_original, :class_name => '::Refinery::Image'
  belongs_to :portfolio, :class_name => 'Portfolio', :foreign_key => 'portfolio_id'
  end
  end
  end

Controller 'admin/exhibit.rb'

 module Refinery
  module Portfolios
   module Admin
    class ExhibitsController < ::Refinery::AdminController
      before_filter :find_all_portfolios

      crudify :'refinery/portfolios/exhibit', :xhr_paging => true

      protected

      def find_all_portfolios
        @portfolios = Refinery::Portfolios::Portfolio.all
      end

    end
  end
 end
end

Controller 'exhibit.rb'

 module Refinery
 module Portfolios
  class ExhibitsController < ::ApplicationController

   before_filter :find_all_exhibits
   before_filter :find_page

  def index
    #@exhibits = Exhibit.all(:include => :portfolio)

    # you can use meta fields from your model instead (e.g. browser_title)
    # by swapping @page for @exhibit in the line below:
    present(@exhibit)
  end

  def show
    @exhibit = Exhibit.find(params[:id])

    # you can use meta fields from your model instead (e.g. browser_title)
    # by swapping @page for @exhibit in the line below:
    present(@exhibit)
  end


  protected

  def find_all_exhibits
    @exhibits = Exhibit.order('position ASC')
  end

  def find_page
    @page = ::Refinery::Page.where(:link_url => "/exhibits").first
  end

   end
  end
 end

Upvotes: 0

Views: 757

Answers (1)

charlesdeb
charlesdeb

Reputation: 601

I am guessing you don't really care about this any more since you asked 10 months ago, but in your exhibit model have you tried changing

attr_accessible :img_original_uid

to

attr_accessible :img_original

The error comes on the img_original field, yet you are giving accessibility to img_original_uid. I am new to dragonfly as well...

Upvotes: 0

Related Questions