I am using activeadmin for car resource and multiple attachments are not entering into records, car records are created successfully but on creation it did not contain attachment. I have two models 'Attachment', models/attachment.rb
class Attachment < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" },default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
And
and my model/car.rb contains following code
class Car < ActiveRecord::Base
has_many :attachments, as: :imageable
accepts_nested_attributes_for :attachments
end
and in my app/admin/car.rb I have following code for multiple attachment.
form do |f|
f.input :make
f.input :model
f.input :color
f.input :engine_type
f.input :description
f.has_many :attachments do |attachment|
attachment.input :attachment, :as => :file
end
f.actions
end
Can anybody please explain how to fix this issue?
I think your input field should be :avatar instead of :attachment
So, it should look like
f.has_many :attachments do |attachment|
attachment.input :avatar, :as => :file
end
Related
How can the association between my two models be saved in ActiveAdmin?
I have two models a room and a photo model. In the ActiveAdmin I want to update the association between the photo and the room.
room.rb (app/models)
class Room < ApplicationRecord
has_many :photos
accepts_nested_attributes_for :photos
end
photo.rb (app/models)
class Photo < ApplicationRecord
belongs_to :room
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
photo.rb (app/admin)
ActiveAdmin.register Photo do
permit_params :image, , rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]
index do
selectable_column
id_column
column :image_file_name
column :listing_name
column :room
column :updated_at
column :created_at
actions
end
show do |image|
attributes_table do
row :image do
photo.image? ? image_tag(photo.image.url, height: '100') : content_tag(:span, "No photo yet")
end
end
active_admin_comments
end
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :image, hint: f.photo.image? ? image_tag(f.photo.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.inputs do
f.collection_select :room, Room.all,:id,:listing_name
end
f.actions
end
The form seems to work but it does not save it to the database when I check the record(last room) in the rails console it always returns me room_id: nil? I have tried everything nothing seems to work. Please help.
UPDATE
I have added, "rooms_attributes: [:id, :listing_name, :room_id, :photo_id, :images]" to the params in the photo.rb(admin). I have also have added :photo_id to the room.rb(admin) file.
But it still does not work! Any hints welcomed! If someone needs further information just let me know.
Try
permit_params :image, room_id
f.collection_select :room_id, Room.all, :id, :listing_name
I have menu model and photos model where the menu has has_many relationship with photo. For the image upload, I'm using paperclip. I was able to build a nested_form that creates photo and other attributes in photos table. However, when I update, the record gets duplicated in photos table and the new photo selected in the update form will not be uploaded. Appreciate your help.
menu model
class Menu < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos, reject_if: :all_blank, allow_destroy: true
end
photo model
class Photo < ActiveRecord::Base
belongs_to :menu
has_attached_file :image,
:styles => { :thumb => "100x100#", :medium => "300x300#", :large => "600x400>" },
:url => "/assets/menus/photos/images/:id/:style/:basename.:extension",
:path => "#{Rails.root}/public/assets/menus/photos/images/:id/:style/:basename.:extension"
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
form.html.haml
= simple_form_for #menu do |f|
= f.simple_fields_for :photos do |photo|
= render 'photo_fields', f: photo
_photo_field.html.haml
.nested-fields
= f.file_field :image
= f.input :main_flag, as: :hidden, input_html: { value: "true" }
= f.input :user_id, as: :hidden, input_html: { value: "1"}
menus_controller.rb
class MenusController < ApplicationController
...
def update
#menu = Menu.find(params[:id])
if #menu.update(menu_params)
if params[:image]
#menu.photos.destroy
#menu.photos.build(menu_params)
end
flash[:success]= 'Menu was successfully updated'
redirect_to brand_menus_path(#menu.brand_id)
else
render 'index'
end
end
private
def menu_params
params.require(:menu).permit(:name, :price, :brand_id, :category_id, :description,
photos_attributes: [:user_id, :image, :main_flag])
end
This is very common problem with the strong_parameters when using with nested_params. Whitelisting the :id in the photos_attributes should fix your problem
def menu_params
params.require(:menu).permit(:name, :price, :brand_id, :category_id, :description,
photos_attributes: [ :id, :user_id, :image, :main_flag])
end
This question already has answers here:
How to make a field required in Rails?
(2 answers)
Closed 7 years ago.
I'm trying to make a file_field required before the form is able to be submitted, using Rails and ERB, and I'm drawing a blank.
<%= form_for Image.new, html: {multipart: true} do |i| %>
<%= i.label :description %>
<%= i.text_field :description %>
<%= i.file_field :image %> <------**** This Line ****
<%= i.submit "Upload" %>
<% end %>
I've tried using 'required' in various ways and searched the web but to no avail. Is this something that I should seek to validate in the model instead?
My model is this:
class Image < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_many :likes, dependent: :destroy
belongs_to :user
has_attached_file :image, :styles => { :large => "600x600>", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
At the bottom of the list of suggested answers I found what I was looking for in this stack thread
validates :image, :presence => true
I simply had to add that to my model and it doesn't let the form submit without an image attachment.
How do I access Image.photo using its id from Pin models view?
Currently I am doing this and it gives me random number if I remove the image_tag or bank image space.
<td><%= image_tag Image.find_by_id(pin.pic) %></td>
This one gives me undefined method `photo' for nil:NilClass
<td><%= image_tag Image.find_by_id(pin.pic).photo.url(:small) %></td>
class Pin < ActiveRecord::Base
attr_accessible :board, :pic, :pin_time, :user, :apic, :photo
belongs_to :auser, :class_name => 'User', :foreign_key => 'user'
belongs_to :apic, :class_name => 'Image',:foreign_key => 'pic'
end
class Image < ActiveRecord::Base
attr_accessible :description, :uid, :web_url, :photo, :photo_file_name,:photo_content_type, :photo_file_size, :photo_updated_at, :id
has_attached_file :photo, :styles => { :small => "150x150>", :medium => "300x300", :large => "600x600"}
belongs_to :user
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
Why not simply
pin.apic.photo.url(:small)
Or are you looking for something else completely?
I want to make polymorphic associations with paperclip, and allow my user to have one avatar and multiple images.
Attachment model:
class Attachment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end
class Avatar < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
class Image < Attachment
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
User Model:
has_one :avatar, :as => :attachable, :class_name => 'Attachment', :conditions => {:type => 'avatar'}
accepts_nested_attributes_for :avatar
User Controller:
def edit
#user.build_avatar
end
User View form:
<%= form_for #user, :html => { :multipart => true } do |f| %>
<%= f.fields_for :avatar do |asset| %>
<% if asset.object.new_record? %>
<%= asset.file_field :image %>
<% end %>
<% end %>
when I attempt to save the changes I get the error => unknown attribute: avatar
if I remove the :class_name => 'attachment' in the has_one association I get the error =>
uninitialized constant User::Avatar
I need to also attach avatars to blog posts, so I need the association to be polymorphic (or atleast i think so)
I am stumped and any help would be greatly appreciated.
I do have a project in the works that is successfully using Paperclip and polymorphic associations. Let me show you what I have, and maybe you can apply it to your project:
class Song < ActiveRecord::Base
...
has_one :artwork, :as => :artable, :dependent => :destroy
accepts_nested_attributes_for :artwork
...
end
class Album < ActiveRecord::Base
...
has_one :artwork, :as => :artable, :dependent => :destroy
accepts_nested_attributes_for :artwork
...
end
class Artwork < ActiveRecord::Base
belongs_to :artable, :polymorphic => true
attr_accessible :artwork_content_type, :artwork_file_name, :artwork_file_size, :artwork
# Paperclip
has_attached_file :artwork,
:styles => {
:small => "100",
:full => "400"
}
validates_attachment_content_type :artwork, :content_type => 'image/jpeg'
end
the songs form and the albums form include this as a partial:
<div class="field">
<%= f.fields_for :artwork do |artwork_fields| %>
<%= artwork_fields.label :artwork %><br />
<%= artwork_fields.file_field :artwork %>
<% end %>
don't forget to include :html => { :multipart => true } with the form
artworks_controller.rb
class ArtworksController < ApplicationController
def create
#artwork = Artwork.new(params[:artwork])
if #artwork.save
redirect_to #artwork.artable, notice: 'Artwork was successfully created.'
else
redirect_to #artwork.artable, notice: 'An error ocurred.'
end
end
end
and finally, an excerpt from songs_controller.rb:
def new
#song = Song.new
#song.build_artwork
end
I'm not sure you really need to be polymorphic. How about this approach, which uses has_many :through? In plain English, the user has one avatar which has multiple images, and through this association you can call User.images to get the collection of images associated with the avatar.
http://guides.rubyonrails.org/association_basics.html
class User < ActiveRecord::Base
has_one :avatar
has_many :images, :through => :avatar
end
class Avatar < ActiveRecord::Base
belongs_to :user
has_many :images
end
class Image < ActiveRecord::Base
belongs_to :avatar
has_attached_file :image, :styles => { :thumb => "150x150>", :view => "260x180>" },
end
Having said all of this, I am left to wonder why you need to go through all this anyway. Why not just do
class User < ActiveRecord::Base
has_many :avatars
end
which would give you as many images (avatars) as you want.