Rails 4 multiple file attachments with Paperclip - ruby-on-rails

I'm aware that there are several posts on Stackoverflow and several tutorials about this subject. None of them however manage to solve my issue and most of them are outdated as well.
I am trying to add multiple images to a project using the paperclip gem in Rails 4.
When i try uploading it i do see the asset attached in the params.
They do not seem to be added to the project_paramns though..
Hope someone can help me out here.
This is my projects_controller
class ProjectsController < ApplicationController
before_filter :find_project, only: [:show, :edit, :update, :destroy]
def index
#projects = Project.all
end
def show
end
def new
#project = Project.new
end
def create
#project = Project.new(project_params)
#project.save
redirect_to project_path(#project)
end
def edit
end
def update
#project.update(project_params)
redirect_to project_path(#project)
end
def destroy
#project.destroy
redirect_to projects_path
end
protected
def project_params
params.require(:project).permit(:name, :description, :asset)
end
def find_project
#project = Project.find(params[:id])
end
end
My project model
class Project < ActiveRecord::Base
has_many :assets, :dependent => :destroy
validates_associated :assets
validates_presence_of :name, :on => :create, :update => "can't be blank"
validates_presence_of :description, :on => :create, :update => "can't be blank"
accepts_nested_attributes_for :assets
end
My asset model
class Asset < ActiveRecord::Base
belongs_to :project
# Paperclip
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" }
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
end
And my form partial (Sorry it's in HAML)
= simple_form_for #project do |f|
%ul
- #project.errors.full_messages.each do |error|
%li= error
.row
.small-1.columns
= f.label :name, :class => "left inline"
.small-11.columns
= f.input_field :name
.row
.small-1.columns
= f.label :description, :class => "left inline"
.small-11.columns
= f.input_field :description, as: :text
.row
= f.simple_fields_for :asset do |a|
.small-1.columns
= a.label :image, :class => "left inline"
.small-11.columns
= file_field_tag :image, multiple: true,
.row
.small-9.small-offset-1.columns
= f.submit nil ,:class => "button [radius round]"
Request Parameters
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"4iK1kUNvKvoJVOKoivz/pcLAe6LY0cUJikQioxa8BIs=", "project"=>{"name"=>"adf", "description"=>"adf", "asset"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007fb808357d80 #tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140607-34739-dvlzt7>, #original_filename="enabling-gzip-compression.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"project[asset][image][]\"; filename=\"enabling-gzip-compression.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007fb808357d58 #tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140607-34739-lwkioi>, #original_filename="minimize_http_requests.png", #content_type="image/png", #headers="Content-Disposition: form-data; name=\"project[asset][image][]\"; filename=\"minimize_http_requests.png\"\r\nContent-Type: image/png\r\n">]}}, "commit"=>"Update Project", "action"=>"update", "controller"=>"projects", "id"=>"11"}
Now i've also got an error showing up in my terminal:
Unpermitted parameters: asset
Edit:
A combination of #pavan's and #kiri thorat's answers have helped me get something showing up in project_params, the output it gives now is:
{"name"=>"Test", "description"=>"Test", "assets_attributes"=>{"0"=>{}}}
Any clue on what's going on here?
After #kirithorat's latest update things seem to be good on the parameter side of things.
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"4iK1kUNvKvoJVOKoivz/pcLAe6LY0cUJikQioxa8BIs=", "project"=>{"name"=>"Test", "description"=>"Test", "assets_attributes"=>{"0"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007fcd383c9a08 #tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140610-36517-7ek1oq>, #original_filename="enabling-gzip-compression.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"project[assets_attributes][0][image]\"; filename=\"enabling-gzip-compression.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}, "commit"=>"Update Project", "action"=>"update", "controller"=>"projects", "id"=>"13"}
The assets are still not being saved though.
Update after implementing #Valikiliy's suggestions
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"4iK1kUNvKvoJVOKoivz/pcLAe6LY0cUJikQioxa8BIs=", "project"=>{"name"=>"Test", "description"=>"Test", "image"=>#<ActionDispatch::Http::UploadedFile:0x007fcd3a08d0b8 #tempfile=#<Tempfile:/var/folders/v_/98sxm8bn24qbqj_jmj40fv400000gn/T/RackMultipart20140610-36517-rgy95n>, #original_filename="minimize_http_requests.png", #content_type="image/png", #headers="Content-Disposition: form-data; name=\"project[image]\"; filename=\"minimize_http_requests.png\"\r\nContent-Type: image/png\r\n">}, "commit"=>"Update Project", "action"=>"update", "controller"=>"projects", "id"=>"16"}
Update: Added new form code on request
= simple_form_for(#project, :html => { :multipart => true }) do |f|
%ul
- #project.errors.full_messages.each do |error|
%li= error
.row
.small-1.columns
= f.label :name, :class => "left inline"
.small-11.columns
= f.text_field :name
.row
.small-1.columns
= f.label :description, :class => "left inline"
.small-11.columns
= f.text_area :description
.row
= f.simple_fields_for :assets do |a|
.small-1.columns
= a.label :image, :class => "left inline"
.small-11.columns
- if a.object.new_record?
= a.input :image, as: :file
- else
= image_tag a.object.image.url(:thumb)
= a.input_field '_destroy', as: :boolean
.row
.small-9.small-offset-1.columns
= f.submit nil ,:class => "button [radius round]"
Update
def project_params
params.require(:project).permit(:name, :description, images: [], assets_attributes: [:_destroy, :id, :image])
end
def find_project
#project = Project.find(params[:id])
#project.assets.build if %w[new edit].include?(action_name)
end
Update: Added model code
class Project < ActiveRecord::Base
has_many :assets, :dependent => :destroy
validates_presence_of :name, :on => :create, :update => "can't be blank"
validates_presence_of :description, :on => :create, :update => "can't be blank"
accepts_nested_attributes_for :assets, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

To save multiple files in the above example you, do not need add multiple: true option, it will cause an error when saving, one file - one record assets table.
For resolve this, you can add multiple file fields, like this:
add into controller:
def new
#project = Project.new
3.times{ #project.assets.build }
end
def edit
3.times{ #project.assets.build }
end
for while list
.permit(..., assets_attributes: [ :_destroy, :id, :image ])
and in view like this:
= f.simple_fields_for :assets do |a|
.small-1.columns
= a.label :image, :class => "left inline"
- if a.object.new_record?
.small-11.columns
= a.file_field :image
- else
.small-11.columns
= image_tag a.object.image.url(:thumb)
= a.input_field '_destroy', as: :boolean
If you try to send multiple images like this:
# view
a.file_field :image, multiple: true
# controller
.permit(..., assets_attributes: [ :_destroy, :id, image: [] ])
This will cause an error because the Asset model does not know what to do with an array of images.
To save more than one file at the same time, you need to use your own handler, for example:
model: add the images= method
def images=(files = [])
assets.create(image: f)
end
controller: move images: [] outside of the assets_attributes
.permit(..., images: [], assets_attributes: [ :_destroy, :id ])
view: remove the fields_for and nested attributes
.row
.small-1.columns
=f.file_field :images, multiple: true
.row
= f.simple_fields_for :assets do |a|
.small-1.columns
= a.label :image, :class => "left inline"
....

You have 1-M relationship between Project and Asset models i.e.,
Project has_many assets
so, in your form partial simple_fields_for should look like
= f.simple_fields_for :assets do |a|
Notice assets in plural and NOT asset in singular
In your current code, you used simple_fields_for with singular asset which is why your params is generated incorrectly and you receive asset key in params hash instead of receiving assets_attributes key which results in the warning as Unpermitted parameters: asset.
Once you correct the form partial you would receive the correct keys in params hash upon form submission. Now, as #Pavan pointed out next problem that I see is you have not permitted the assets_attributes correctly in the controller.
You need to update the project_params method as below:
def project_params
params.require(:project).permit(:name, :description, assets_attributes: [:image])
end
Notice assets_attributes with assets in plural
UPDATE
You would need to add #project.assets.build in new and edit action of ProjectsController in order to see the fields for assets in the new and edit view.
Also, I would suggest adding :id in the list of permitted attributes for assets_attributes in project_params as below:
params.require(:project).permit(:name, :description, assets_attributes: [:id, :image])
Few more problems that I see is in the form partial are
As you are uploading a file, you should specify :html => {:multipart => true} in the form.
Change
= simple_form_for #project do |f|
To
= simple_form_for #project, :html => {:multipart => true} do |f|
Project has_many assets and every asset record would have only one image, so remove multiple: true. Also, as you are using simple_form, its advisable to use simple_form helper method for uploading file.
Change
= file_field_tag :image, multiple: true,
To
= a.input :image, as: :file

Your project_params should be like this
def project_params
params.require(:project).permit(:name, :description, assets_attributes: [:image])
end
And also,why you are using protected? i guess you should be using private.

Related

Why won't update action work for nested form fields in rails?

I have a job model that belongs_to a profile model. A profile has_many jobs. I have a nested model form that in which a user adds jobs. The form is successfully adding jobs, but editing/updating is not working. Instead, when I try to edit a job, it keeps the old version of the job, but adds the new version as well. It does not replace the old version with the new one. How do I fix this? I'm pretty sure it has something to do with the edit/update controllers.
Edit controller:
def edit
#profile = current_user.profile
end
Update controller:
def update
#if current_user.profile.jobs.any?
#profile = current_user.profile.update_attributes(profile_params)
if current_user.profile.invalid?
#profile = current_user.profile
render :edit, :status => :unprocessable_entity
else
redirect_to profile_path(current_user.profile_name)
end
end
The thing is, the update controller is working for the non-nested information, it is just not working for the nested jobs. Here are the strong parameters:
def profile_params
params.require(:profile).permit(:title,
:category, :description, :state, :zip_code, :rate,
jobs_attributes: [:firm, :position, :category, :description,
:begin, :end, :_destroy])
end
And here is the profile model:
class Profile < ActiveRecord::Base
belongs_to :user
has_many :jobs
accepts_nested_attributes_for :jobs , :reject_if => :all_blank, :allow_destroy => true
end
Also, here's my routes if that will help:
resources :profiles do
resources :jobs
end
Thanks for your help in advance.
EDIT
Here are the params from the create action:
{"jobs_attributes"=>{"1383593195849"=>{"firm"=>"1st firm", "position"=>"1st position",
"category"=>"1st category", "description"=>"1st description", "begin"=>"1999",
"end"=>"1999", "_destroy"=>"false"}}}
And here are the params for the same job when updated:
{"jobs_attributes"=>{"0"=>{"firm"=>"1st firm", "position"=>"1st position",
"category"=>"1st category", "description"=>"1st description", "begin"=>"1999",
"end"=>"1999", "_destroy"=>"false"}, "1"=>{"firm"=>"1st firm",
"position"=>"1st position", "category"=>"1st category",
"description"=>"1st description", "begin"=>"1999", "end"=>"1999", "_destroy"=>"1"}}}
EDIT:
Here are my views. I don't think they are part of the problem though.
= simple_form_for #profile do |f|
%h3 Jobs
#jobs
= f.simple_fields_for :jobs do |job|
= render 'job_fields', :f => job
.links
= link_to_add_association 'add job', f, :jobs
= f.submit
And here is the "job_fields" partial:
.nested-fields
= f.input :firm
= f.input :position
= f.input :category
= f.input :begin
= f.input :end
= f.input :description
= f.input :_destroy, as: :boolean, inline_label: 'Delete box'
= link_to_remove_association "remove task", f
The trick is adding the ':id' symbol to the strong params. Although I still haven't figured out why and I'm not sure if its secure.

Rails 3.2 has_many :as polymorphic, Simple Form, simple_fields_for

Trying to save some images in my product form. I'm excpecting paremeters where I would have images_attributes being a part of "product". When I make parameters like that in the console and create a Product, the images actually save.
class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
attr_accessible :description, :name, :category_ids, :brand_ids, :image_ids, :images_attributes
has_many :images, :as => :imageable
accepts_nested_attributes_for :images
end
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
attr_accessible :name, :file
mount_uploader :file, ImageUploader
end
= simple_form_for(#product, :html => {:multipart => true}) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.input :description
= f.association :categories, as: :check_boxes
= f.association :brands, as: :check_boxes
= f.association :images
= simple_fields_for :images do |i|
= i.input :file, as: :file
= i.input :name
.form-actions
= f.button :submit
# GET /products/new
# GET /products/new.json
def new
#product = Product.new
#product.images.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: #product }
end
end
{
"utf8"=>"✓",
"authenticity_token"=>"vvXZFh9sJivA3i4Y0rx9i/oqLwKByrExgYisfdj/N78=",
"product"=> {
"name"=>"sxsad",
"description"=>"saasd",
"category_ids"=>[""],
"brand_ids"=>[""],
"image_ids"=>[""]
},
# should be images_attributes and come straight after image_ids?
"images"=>{
"name"=>"sdfsdfsdf"
},
"commit"=>"Create Product"
}
Once i've got it working for one image I will look into something like Cocoon for multiple images. Any thoughts on where this could be going wrong would be greatly appreciated :).
You should simply write:
= f.simple_fields_for :images do |i|

Ruby on Rails, form_for, paperclip, and mass-assignment of protected parameters

I have been developing a rails app that uploads and processes images. Images, along with other string information is submitted via a form_for. I've been researching this topic for about 16 hours now and no solution has worked. Honestly it's like rails isn't even reading my code.
One Processmodel has many Assets, where an Asset is just a model to hold one image file. When creating processmodels, I can never access the asset, always recieving the cannot mass-assign attirbutes: assets_attributes
Completed 500 Internal Server Error in 13ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: asset):
app/controllers/process_controller.rb:20:in `new'
app/controllers/process_controller.rb:20:in `create'
-
This form is used in new.html.erb
<%= semantic_form_for #processmodel, :url => { :action => 'create' }, :html => { :multipart => true } do |f| %>
<%= f.input :batch, :as => :string, :name => "Batch" %>
<%= f.input :batchset, :as => :string, :name => "Batchset" %>
<%= f.input :numSlots, :as => :number, :name => "Number of slots" %>
<%= f.input :key, :as => :file, :name => "Key" %>
<%= f.semantic_fields_for :asset do |asset| %>
<%= asset.input :asset, :as => :file, :label => "Image" %>
<% end %><br />
<%= f.submit %>
<% end %>
-
class Processmodel < ActiveRecord::Base
attr_accessible :user_id, :batch,
:batchset, :numSlots,
:key,:assets_attributes
attr_accessor :key_file_name
has_many :assets, :dependent => :destroy
belongs_to :user
has_attached_file :key
# :url => Rails.root.join('/assets/readimages/:basename.:extension'),
# :path => Rails.root.join('/assets/readimages/:basename.:extension'),
accepts_nested_attributes_for :assets, :allow_destroy => true
.
.
.
end
-
require 'RMagick'
class Asset < ActiveRecord::Base
attr_accessible :results_string,
:name,
:ambiguous_results,
:image
belongs_to :batch_element
belongs_to :processmodel
has_attached_file :image
validates_attachment_presence :image
end
-
class ProcessController < ApplicationController
def create
#Processmodel = Processmodel.new(params[:processmodel])
#Processmodel.save
all_img = Array.new(#processmodel.assets.all)
respond_to do |format|
if #processmodel.beginRead(...)
redirect_to :action => 'results_main', :controller => 'results'
else
format.html { render action: "new" }
end
end
end
-
def new
#processmodel = Processmodel.new
#5.times{#processmodel.assets.build}
respond_to do |format|
format.html #new.html.erb
end
end
Am requesting an ideas on how to fix this and get my app working.
You need to update your database migration. Run:
rails g migration AddIdToAsset processmodel_id:integer
rake db::migrate
You've called your attached file :image here:
has_attached_file :image
But you call it :asset in your view:
<%= asset.input :asset, :as => :file, :label => "Image" %>
To fix, just change this line to
<%= asset.input :image, :as => :file, :label => "Image" %>

Paperclip: "missing" image

I'm working on a website that allows people who run bed and breakfast businesses to post their accommodations.
I would like to require that they include a "profile image" of the accommodation when they post it, but I also want to give them the option to add more images later (this will be developed after).
I thought the best thing to do would be to use the Paperclip gem and have a Accommodation and a Photo in my application, the later belonging to the first as an association.
A new Photo record is created when they create an Accommodation. It has both id and accommodation_id attributes. However, the image is never uploaded and none of the Paperclip attributes get set (image_file_name: nil, image_content_type: nil, image_file_size: nil), so I get Paperclip's "missing" photo.
Any ideas on this one? It's been keeping me stuck for a few days now.
Accommodation
models/accommodation.rb
class Accommodation < ActiveRecord::Base
validates_presence_of :title, :description, :photo, :thing, :location
attr_accessible :title, :description, :thing, :borough, :location, :spaces, :price
has_one :photo
end
controllers/accommodation_controller.erb
class AccommodationsController < ApplicationController
before_filter :login_required, :only => {:new, :edit}
uses_tiny_mce ( :options => {
:theme => 'advanced',
:theme_advanced_toolbar_location => 'top',
:theme_advanced_toolbar_align => 'left',
:theme_advanced_buttons1 => 'bold,italic,underline,bullist,numlist,separator,undo,redo',
:theme_advanced_buttons2 => '',
:theme_advanced_buttons3 => ''
})
def index
#accommodations = Accommodation.all
end
def show
#accommodation = Accommodation.find(params[:id])
end
def new
#accommodation = Accommodation.new
end
def create
#accommodation = Accommodation.new(params[:accommodation])
#accommodation.photo = Photo.new(params[:photo])
#accommodation.user_id = current_user.id
if #accommodation.save
flash[:notice] = "Successfully created your accommodation."
render :action => 'show'
else
render :action => 'new'
end
end
def edit
#accommodation = Accommodation.find(params[:id])
end
def update
#accommodation = Accommodation.find(params[:id])
if #accommodation.update_attributes(params[:accommodation])
flash[:notice] = "Successfully updated accommodation."
render :action => 'show'
else
render :action => 'edit'
end
end
def destroy
#accommodation = Accommodation.find(params[:id])
#accommodation.destroy
flash[:notice] = "Successfully destroyed accommodation."
redirect_to :inkeep
end
end
views/accommodations/_form.html.erb
<%= form_for #accommodation, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<p>
Title<br />
<%= f.text_field :title, :size => 60 %>
</p>
<p>
Description<br />
<%= f.text_area :description, :rows => 17, :cols => 75, :class => "mceEditor" %>
</p>
<p>
Photo<br />
<%= f.file_field :photo %>
</p>
[... snip ...]
<p><%= f.submit %></p>
<% end %>
Photo
The controller and views are still the same as when Rails generated them.
models/photo.erb
class Photo < ActiveRecord::Base
attr_accessible :image_file_name, :image_content_type, :image_file_size
belongs_to :accommodation
has_attached_file :image,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end
To create an upload with paperclip, you need to use the name you provided for the has_attached_file line, on the model you defined it on. In your case, this will result in this view code:
<%= form_for #accommodation, :html => { :multipart => true } do |f| %>
<%= f.fields_for :photo do |photo_fields| %>
<p>
Photo<br />
<%= photo_fields.file_field :image %>
</p>
<% end %>
<% end %>
In the controller:
class AccommodationsController < ApplicationController
# also protect create and update actions!
before_filter :login_required, :only => [ :new, :create, :edit, :update ]
def new
# always make objects through their owner
#accommodation = current_user.accommodations.build
#accommodation.build_photo
end
def create
#accommodation = current_user.accommodations.build(params[:accommodation])
if #accommodation.save
# always redirect after successful save/update
redirect_to #accommodation
else
render :new
end
end
end
Tell Rails to handle the nested form:
class Accommodation
has_one :photo
accepts_nested_attributes :photo
attr_accessible :photo_attributes, :title, :description, :etc
end
And make sure to set the accessible attributes right in your photo model:
class Photo
attr_accessible :image # individual attributes such as image_file_name shouldn't be accessible
has_attached_file :image, :styles => "etc"
end
Be sure to watch your log files to spot things that are protected by attr_accessible, but still are in your form.

rails 3, paperclip (& formtastic) - deleting image attachments

I can't seem to find an example that is complete in all the components. I am having a hard time deleting image attachments
Classes
class Product
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images
end
class ProductImage
belongs_to :product
has_attached_file :image #(etc)
end
View
<%= semantic_form_for [:admin, #product], :html => {:multipart => true} do |f| %>
<%= f.inputs "Images" do %>
<%= f.semantic_fields_for :product_images do |product_image| %>
<% unless product_image.object.new_record? %>
<%= product_image.input :_destroy, :as => :boolean,
:label => image_tag(product_image.object.image.url(:thumb)) %>
<% else %>
<%= product_image.input :image, :as => :file, :name => "Add Image" %>
<% end %>
<% end %>
<% end %>
<% end %>
Controller
class Admin::ProductsController < AdminsController
def edit
#product = Product.find_by_permalink(params[:id])
3.times {#product.product_images.build} # added this to create add slots
end
def update
#product = Product.find_by_permalink(params[:id])
if #product.update_attributes(params[:product])
flash[:notice] = "Successfully updated product."
redirect_to [:admin, #product]
else
flash[:error] = #product.errors.full_messages
render :action => 'edit'
end
end
end
Looks good, but, literally nothing happens when I check the checkbox.
In the request I see:
"product"=>{"manufacturer_id"=>"2", "size"=>"", "cost"=>"5995.0",
"product_images_attributes"=>{"0"=>{"id"=>"2", "_destroy"=>"1"}}
But nothing gets updated and the product image is not saved.
Am I missing something fundamental about how 'accepts_nested_attributes_for' works?
From the API docs for ActiveRecord::NestedAttributes::ClassMethods
:allow_destroy
If true, destroys any members from the attributes hash with a _destroy key and a value that evaluates to true (eg. 1, ‘1’, true, or ‘true’). This option is off by default.
So:
accepts_nested_attributes_for :product_images, allow_destroy: true

Resources