Active Admin and instantiating a new object - ruby-on-rails

I have two tables: Event and Image
Events:
id
name
date
location
Images:
id
file
caption
event_id
An Event has many images and an Image belongs to an Event.
My ultimate goal is to upload multiple images at once in Active Admin. I have read several other posts suggesting I do the following:
class Event < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images
mount_uploader :file, AvatarUploader
end
class Image < ActiveRecord::Base
belongs_to :event
mount_uploader :file, AvatarUploader
end
Then, in my admin/event.rb, I have the following:
form do |f|
f.inputs "Event Details" do
f.input :name
f.input :location
f.input :date
f.has_many :images do |p|
p.input :file, :as => :file, :for => [Image.new]
end
end
f.actions
I am able to visually get multiple image upload fields, the problem is that when I save, the images actually aren't saving. I believe this is because I am not creating a new image (Image.new) on each file upload. Is there a way to do this in active admin?
Thanks

Related

Can`t Update or Delete when using Active Admin

I would like to ask how to customize active admin.
I`m making my own blog and creating admin page using active admin gem.
This has many to many relationship with article and tag through article_tag table.
What I want to do is to add tag from article panel and I was able to show tag view in article panel, but it is not working fine.(I can`t update or remove tag after save once)
http://localhost:3000/admin/articles/new
image
I made the many to many relation with Article and Tag model like this.
article.rb
class Article < ActiveRecord::Base
has_many :article_tags
has_many :tags, through: :article_tags
accepts_nested_attributes_for :article_tags, :allow_destroy => true
end
tag.rb
class Tag < ActiveRecord::Base
has_many :article_tags
has_many :articles, through: :article_tags
end
article_tag.rb
class ArticleTag < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
And I customized active admin like this.
ActiveAdmin.register Article do
permit_params :title, :description, :url, :image_url, :media, article_tags_attributes: [:article_id, :tag_id, :name, :_destroy, :_edit]
form do |f|
# f.semantic_errors *f.object.errors.keys
f.inputs "Admin Details" do
f.input :title
f.input :description
f.input :url
f.input :image_url
f.input :media
f.input :publish
end
f.inputs "Articles" do
f.has_many :article_tags do |t|
t.input :tag
end
end
f.actions
end
end
But after I saved the article with tag once I can`t update tag or delete tag...
Does anyone know how to fix this?
You forgot to permit :id attribute of article_tag object. It is passed when updating/deleting existing nested object.
ActiveAdmin.register Article do
permit_params :title, :description, :url, :image_url, :media, article_tags_attributes: [:id, :article_id, :tag_id, :name, :_destroy, :_edit]
...
end

rails list all items for a many to many including design

So I have the following models:
Image:
class Image < ActiveRecord::Base
has_many :product_images
has_many :products, :through => :product_images
attr_accessible :asset, :name, :description, :product_ids, :file_content_type, :is_boolean
accepts_nested_attributes_for :product_images
has_attached_file :asset
end
ProductImage:
class ProductImage < ActiveRecord::Base
belongs_to :product
belongs_to :image
attr_accessible :is_thumbnail
end
and Product:
class Product < ActiveRecord::Base
has_many :images, :through => :product_images
has_many :product_images
attr_accessible :name, :description, :thumbnail, :searchTerms, :group_ids, :upload_file_ids
end
Now what I would like to do on the images form is display a checkbox for all the products and then another checkbox for the is_thumbnail attribute
I have had a look into using simple_fields_for but this will only display if the product has already been added. Is there a way to do this?
<%= f.simple_fields_for(:product_images) do |builder| %>
<%= builder.input :is_thumbnail %>
<%= builder.association :products, include_blank: false %>
<% end %>
I'm not very familiar with simple_fields however building form inputs base on an instance will only allow you to "represent" that instance.
This means that you could print all already associated products using
builder.association :products
but if you want to print all products in your database you will need to fetch them, loop and display them in your form.

active_admin and adding multiple images to gallery

I'm using active_admin and carrierwave gems. Have two simple models:
class Image < ActiveRecord::Base
attr_accessible :gallery_id, :file
belongs_to :gallery
mount_uploader :file, FileUploader
end
class Gallery < ActiveRecord::Base
attr_accessible :description, :title, :file, :images_attributes
has_many :images
accepts_nested_attributes_for :images, allow_destroy: true
mount_uploader :file, FileUploader
end
Now my active_admin form for Gallery looks like this:
form do |f|
f.inputs "Gallery" do
f.input :title
end
f.has_many :images do |ff|
ff.input :file
end
f.actions
end
Now I can upload one file, click "Add New Image" and upload another one. Instead of it, I'd like to click "Add new Image", select multiple files and upload them all at once. Any idea how can I implement it?
For a Gallery form with multiple image uploads you can try this
admin/galleries.rb
form do |f|
f.inputs "Gallery" do
f.input :name
end
f.has_many :images do |ff|
ff.input :file
end
end
In model/gallery.rb:
attr_accessible :images_attributes
In model/gallery.rb (add after relations):
accepts_nested_attributes_for :images, :allow_destroy => true

Rails 3 file upload with Carrierwave and Formtastic

I have a Product with many Photos. The relationship is this way:
product.rb
class Product < ActiveRecord::Base
has_many :photos, :dependent => :destroy
attr_accessible :translations_attributes, :active, :category_id, :photos
accepts_nested_attributes_for :translations, :photos
photo.rb
class Photo < ActiveRecord::Base
attr_accessible :photo, :product_id
belongs_to :attachable
mount_uploader :photo, PhotoUploader
and the form looks like this:
form(:html => { :multipart => true }) do |f|
f.inputs "Details" do
f.input :active
f.input :category, :include_blank => false
end
f.inputs "Photos" do
f.input :photos, :as => :file
end
f.buttons
end
The problem is that I get this error when creating/updating the product with a file attached:
undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x007f93c8549828>
I found a question with the same error here, but even trying their solution still have the same problem.
I'm using Friendly_Id, ActiveAdmin, Globalize3, maybe it could have some relationship with them, this is my first project with Rails and right now I don't know what could be...
Your problem is that you're declaring photos as a has_many association in your model, and declaring it as a single file input in your view. Many or one, take your pick :)
What you're probably trying to do is have photos as a nested model in your Product form - something like so:
semantic_form_for #product do |f|
f.semantic_fields_for :photos do |ff|
ff.input :photo, as: :file
To loop over each of the photos in your product, and display a file input field for each.

Carrierwave saves object data to database instead of file name

Hi
I just can not find out what is wrong with my code. I have two models Items and Images and relation between them
class Item < ActiveRecord::Base
attr_accessible :category_id, :user_id, :title, :description, :published, :start_date, :end_date, :images_attributes
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
mount_uploader :name, ImageUploader
end
class Image < ActiveRecord::Base
belongs_to :item
mount_uploader :image, ImageUploader
attr_accessible :item_id, :name
end
I call the 3 instances of Carrierwave in items_controller.rb to add 3 images to the created item
def new
#item = Item.new
3.times { #item.images.build }
end
The view form looks like this :
<%= f.fields_for :images do |builder| %>
<p> <%= builder.text_field :name %> </p>
<% end %>
Which resoults in this randered code :
<input id="item_images_attributes_0_name" name="item[images_attributes][0][name]" type="file">
When Add and save the new item i get object data saved instead of file name ( suit.jpg ) in my database :
--- !ruby/object:ActionDispatch::Http::UploadedFile
content_type: image/jpeg
headers: |
Content-Disposition: form-data; name="item[images_attributes][0][name]"; filename="suit.jpg"
Content-Type: image/jpeg
original_filename: suit.jpg
tempfile: !ru
Screen shot from database table below :
https://lh3.googleusercontent.com/_USg4QWvHRS0/TXDT0Fn-NuI/AAAAAAAAHL8/91Qgyp5jK3Q/carrierwave-objest-database-saved.jpg
Is anyone who have an idea how to solve it ?
I had a similar issue, I wanted to create my own filenames (identifiers) so I defined a filename method on the uploader (in your case ImageUploader)
e.g.
def filename
#name ||= "foo"
end
First things first, Your mounting your uploader to a column named :image but from your pic of your db you dont have a column with said name.
1: Make a column for images called image ("since thats what your uploading.")
rails g migration add_image_to_images image:string
rake db:migrate
2: Update your model's attr_accessible to use the new column.
class Image < ActiveRecord::Base
belongs_to :item
mount_uploader :image, ImageUploader
attr_accessible :item_id, :name, :image
end
3: Update your view
<%= f.fields_for :images do |builder| %>
<p>
<%= builder.text_field :name %>
<%= builder.file_field :image %>
</p>
<% end %>
4: remove the unused mount from the Item class.
class Item < ActiveRecord::Base
attr_accessible :category_id, :user_id, :title, :description, :published, :start_date, :end_date, :images_attributes
has_many :images, :dependent => :destroy
accepts_nested_attributes_for :images, :reject_if => :all_blank, :allow_destroy => true
end
I left :name in place on Image for use as an arbitrary value you can add to your image.
Also by abstracting your image model like you did I would also assume you want to keep track of the image order so maybe an extra column for that would be a good idea too.
Hope this helps.

Resources