Carrierwave saves object data to database instead of file name - carrierwave

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.

Related

How can I mass assign parameters in a polymorphic association with Paperclip?

I have this code in one of my Models
class Admin::About < ActiveRecord::Base
attr_accessible :address1, ..., :assets_attributes
has_many :assets, as: :imageable
accepts_nested_attributes_for :assets
This in the Polymorphic table
class Admin::Asset < ActiveRecord::Base
attr_accessible :imageable_id, :imageable_type, :position, :image
belongs_to :imageable, polymorphic: true
has_attached_file :image
And in the form I added this
<%= f.fields_for :admin_assets do |asset_fields| %>
<%= asset_fields.file_field :image %>
<% end %>
When I submit the data with the image I got this error
Can't mass-assign protected attributes: admin_assets
I can't find the problem. Perhaps something is missing?
There is no admin_assets attribute. I think you mean assets_attributes. It's what you have defined here accepts_nested_attributes_for :assets and then above in attr_accessible :address1, ..., :assets_attributes.
Change your fields_for to:
<%= f.fields_for :assets_attributes do |asset_fields| %>

RoR - Searching gem for uploading files

I'm searching a gem (like paperclip or carrierwave) that allows you to upload images. But I want a relationship between the image and the otherside:
I have a table day
And I want a own table for the images so that at the and I'm going to have an has_many belongs_to relationship.
So one day has many images
any suggestions?
If I understand you correctly, you may want to look at the has_many :through relationship for this. We have this working with Paperclip & allows you to have many images to one record:
#app/models/day.rb
Class Day > ActiveRecord::Base
has_many :day_images, :class_name => "DayImage"
has_many :images, :class_name => "Image", :through => :day_images, dependent: :destroy
accepts_nested_attributes_for :day_images, :allow_destroy => true
end
#app/models/day_image.rb
Class DayImage > ActiveRecord::Base
belongs_to :day, :class_name => "Day"
belongs_to :image, :class_name => "Image"
accepts_nested_attributes_for :image, :allow_destroy => true
end
#app/models/image.rb
Class Image > ActiveRecord::Base
has_many :day_images, :class_name => "DayImage"
has_many :days, :class_name => "Day", :through => :day_images, dependent: :destroy
end
The join model would then look like this:
day_images table
id | day_id | image_id | extra attribute | extra attribute | created_at | updated_at
This would allow you to allocate images to the day model using the accepts_nested_attributes_for, like this:
Nested Models
Nested models are pretty tough to get right at first, but get easier the more you do them
Using the models I outlined above, you'll have to add several important factors to make accepts_nested_attributes_for work for you. Here's how:
#app/controllers/days_controller.rb
def new
#day = Day.new
#day.day_images.build.build_image
end
def create
#day = Day.new(day_params)
#day.save
end
private
def day_params
params.require(:day).permit(:day, :variables, day_images_attributes: [:image_id, :extra_attributes, :in, :join, :model, image_attributes: [:image]])
end
This will allow you to create a form like this:
#app/views/days/new.html.erb
<%= form_for #day do |f| %>
<%= f.text_field :day_attribute %>
<%= f.fields_for :day_images do |day_image| %>
<%= day_image.text_field :caption %>
<%= day_image.collection_select(:image_id, Image.where(:user_id => current_user.id), :id, :image_name, include_blank: 'Images') %> --> this will allow you to assign images
<%= day_image.fields_for :image do |i| %>
<%= i.file_field :image %> --> uploads new image
<% end %>
<% end %>
<% end %>
This is all based off live code. If you need any more help, let me know!

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

CarrierWave save NULL at file column

im using model called Photo that references Uploader class..
class Photo < ActiveRecord::Base
attr_accessible :title, :album_id
belongs_to :album
mount_uploader :photo_image, PhotosUploader
end
class Album < ActiveRecord::Base
attr_accessible :title, :autor, :photos_attributes
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos
end
but.. when i try to save new Album (or edit, whatever..) with image it not save a file (the collumn photo_image is saved as NULL and file not stored too.
... views/albums/_form.html.erb
<%= f.fields_for :photos do |f| %>
<div class="field">
<%= f.label :photo_image %><br />
<%= f.file_field :photo_image %>
</div>
<% end %>
any suggestion?
just try to add :photo_image to attr_accessible at Photo model, it's maybe could help you.
Also check your log/development.log

Resources