Nested forms dont work in rails_admin - ruby-on-rails

Brand has many images is my association. Image uses paperclip
In Rails Admin, I want to add images when i add a brand
class Brand < ActiveRecord:Base
has_many :images, as=> :imageable
end
class Image < ActiveRecord:Base
attr_accessor :image_thumb
attr_accessible :image, :imageable_id, :imageable_type, :image_thumb
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
belongs_to :imageable, :polymorphic => true
end
But this is what i get
How can i achieve my goal?

I need to add attr_accessible
class Brand < ActiveRecord::Base
attr_accessible :name, :images_attributes
has_many :products
has_many :images, :as => :imageable
accepts_nested_attributes_for :images, :allow_destroy => true
end

And solution is when you user acts_as_taggable, to add to your rails_admin.rb config file next code:
field :tag_list do
label "Tag"
end
Or 'image_list', depends on what you are use.

Related

interpolating polymorphic relation type in url paperclip

I have a photo model which is polymorphic
class Photo < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image,
:styles => {:thumb => '120x120>', :medium => '640x480>' },
:default_style => :thumb,
:url => "/uploads/photos/#{model.imageable_type.to_s.pluralize.underscore}/:id/:basename.:extension"
and
class Question < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
class Answer < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
this generates the url as uploads/photos/Photo/some_id/file_name.ext
but I want
public/uploads/photos/questions/1/image.jpg
and
public/uploads/photos/answers/1/image.jpg.
Solved it myself.
:url => "/uploads/photos/:imageable_name/:id/:basename.:extension"
Paperclip.interpolates :imageable_name do |attachment, style|
attachment.instance.imageable_type.pluralize.downcase
end

How to validate attachment in nested model?

I have a Design model that has two Paperclip attachments inside two associations (full_image and preview). I want to be able to save designs only when both full_image and preview have valid files but can't seem to be able to make it work. Right now this is what I expected to work, but it doesn't just doesn't validate the attachments when I submit the form.
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_associated :preview, :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
class Preview < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
Could someone please suggest what I should be doing?
Try :
validates :attachment, :presence => true
inside the associated model instead of validates_attachment_presence
Here is how I got it working
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_presence_of :preview
validates_presence_of :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
end
class Preview < Asset
has_attached_file :attachment
end

Rails model multiple properties of the same model

I'm struggle to find a way to have more than one property of the same model. I've this structure :
class Image < ActiveRecord::Base
attr_accessible :name, :content_type
end
class MenuImage < ActiveRecord::Base
belongs_to :image
belongs_to :menu
end
class Menu < ActiveRecord::Base
has_many :menu_images
has_many :images, :through => :menu_images
has_one :image, :as => :thumbnail_image
attr_accessible :thumbnail_image_id
end
I want to be able to access the images using #menu.images and #menu.thumbnail_image.
The code has_one :image, :as => :thumbnail_image is an example of what i'm trying to obtain.
You name it differently, but tell ActiveRecord to use the Image class:
has_one :thumbnail_image, :class_name => "Image"
See also the Association Guide: http://guides.rubyonrails.org/association_basics.html#has_one-association-reference

Paperclip attachment shared by 2 models

I am trying to roll out my own e-commerce solution. Extending the depot application explained in the Pragmatic Web Development with Rails book.
I am currently trying to figure out attachments.
Essentially I want Product and Product_Variants to use Product_Shots for attached photos.
This could result in the product_shots table with empty values for product_variants, because not all products have prodcut_variants. Is there a better approach to implement this?
Product model:
class Product < ActiveRecord::Base
validates :title, :description, :price, :presence=>true
validates :title, :uniqueness => true
validates :price, :numericality =>{:greater_than_or_equal_to => 0.01}
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
has_and_belongs_to_many :product_categories
has_many :product_variants
has_many :product_shots, :dependent => :destroy
accepts_nested_attributes_for :product_shots, :allow_destroy => true,
:reject_if => proc { |attributes| attributes['shot'].blank?
}
private
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, "Line items present")
end
end
end
Product Variant model
class ProductVariant < ActiveRecord::Base
belongs_to :product
belongs_to :product_categories
has_many :variant_attributes
has_many :product_shots # can I do this?
end
Product Shots model (handled by Paperclip)
class ProductShot < ActiveRecord::Base
belongs_to :product, :dependent =>:destroy
#Can I do this?
belongs_to :product_variant, :dependent => :destroy
has_attached_file :shot, :styles => { :medium => "637x471>",
:thumb => Proc.new { |instance| instance.resize }},
:url => "/shots/:style/:basename.:extension",
:path =>":rails_root/public/shots/:style/:basename.:extension"
validates_attachment_content_type :shot, :content_type => ['image/png', 'image/jpg', 'image/jpeg', 'image/gif ']
validates_attachment_size :shot, :less_than => 2.megabytes
### End Paperclip ####
def resize
geo = Paperclip::Geometry.from_file(shot.to_file(:original))
ratio = geo.width/geo.height
min_width = 142
min_height = 119
if ratio > 1
# Horizontal Image
final_height = min_height
final_width = final_height * ratio
"#{final_width.round}x#{final_height.round}!"
else
# Vertical Image
final_width = min_width
final_height = final_width * ratio
"#{final_height.round}x#{final_width.round}!"
end
end
end
If I were to implement this, I think I would do it as a polymorphic relationship. So in product.rb and product_variant.rb:
has_many :product_shots, :dependent => :destroy, :as => :pictureable
And in product_shot.rb:
belongs_to :pictureable, :polymorphic => true
Now either product and product_variant can have as many (or as few) product_shots as they want, and both are accessible as product.product_shots and product_variant.product_shots. Just make sure you set up your database correctly -- your product_shots table will need pictureable_type and pictureable_id for this to work.

polymorphic association and setting default value for an asset

Newbie question. I have the following models:
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
#paperclip
has_attached_file :asset,
:hash_secret => "my-secret",
:url => "/images/:hash_:basename_:style.:extension",
:path => UPLOAD_PATH + "/:hash_:basename_:style.:extension",
:styles => { :medium => "300x300>", :thumb => "75x75>"
}
end
class Location < ActiveRecord::Base
has_many :assets, :as => :assetable, :dependent => :destroy
end
class MenuItem < ActiveRecord::Base
has_many :assets, :as => :assetable
end
My asset has a property called description. If the assetable_type is a "MenuItem" and the description is nil, I'd like the description to be the associated menu_item's body. How would I do this?
thx
class Asset < ActiveRecord::Base
before_save :set_description
private
def set_description
self.description ||= assetable.body if assetable.is_a?(MenuItem)
end
end
Or modify the accessor
def description
return self[:description] unless self[:description].blank?
assetable.description if assetable.is_a? MenuItem
end

Resources