我从宝贵的资源 stackoverflow 中学到了很多东西。我非常感谢做出的许多出色贡献。开源和使其成为现实的开源万岁!
对于这里的所有重要信息,我无法在这里找到解决我的问题的正确方法。让我解释。
使用 RefineryCMS v. 2.0.9 我在 Rails 应用程序中生成了一个引擎,用于呈现“投资组合”。'投资组合' has_many ':exhibits'。':exhibit' belongs_to ':portfolio' 根据以下模型。
模型属性 :img_orginal - 包含 Dragonfly 图像上传的 uid。这可以正常工作,图像上传 uid 已提交到数据库,并且可以按预期调用到 views/portfolios/show.html.erb 视图。该网站旨在响应。如果要响应,它需要一套从原始生成的调整大小的拇指。这些需要在 :img_original 通过后端表单上传和提交时由 Dragonfly 生成。到目前为止,我无法在展览模型中使用 Dragonfly image_accessor 来实现这一点。我在创建尝试时收到以下异常。
无法批量分配受保护的属性:img_original ActiveModel::MassAssignmentSecurity::Error in Refinery::Portfolios::Admin::ExhibitsController#create
两个数据库都存在并且通过“exhibits”模型中的“:portfolio_id”外键正确链接。根据当前的 Rails 最佳实践,所有模型属性都被列入白名单。
我错过了什么?我认为控制器方法不正确,我无法弄清楚它们应该如何。是否需要修改引擎控制器以在实例中调用 Page Image 类或 Dragonfly 本身?我需要更改 routes.rb。有人可以建议我如何定义控制器的创建和更新方法。是否有必要,因为它们应该已经由主要炼油厂应用程序的 page_images 控制器定义。
我已经使用 image_accessor 尝试了 Dragonfly 文档中建议的方法,但无济于事。以下是迄今为止采取的一些方法。
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.
我对此很陌生,到目前为止,我一直在努力研究以实现它。然而我来到这个并且无法破解它。我将不胜感激任何人可能提供的帮助。谢谢你。
下面的代码示例和环境信息。
环境; Ubuntu 12.10 RubyMine IDE Ruby 1.9.3 RVM 处理 gems Rails 3.2.9 SQLite 开发数据库 RefineryCMS(包括 page_images 和查询插件) Dragonfly 0.9.14
使用这些迁移生成的投资组合引擎
# 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
和
# 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
并且refinery_portfolios_exhibits 表列标题已更改
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.rb' has_many 展示模块 Refinery 模块 Portfolios 类 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.rb”;属于_to 投资组合
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
控制器'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
控制器'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