Using paperclip gem in rails3 - ruby

Using paperclip gem in rails3, there are two copies of image uploaded simultaneously of which one is having null entries and the other is original in the database as I checked in localhost/phpmyadmin. This problem unnecessarily populates my database. Have been searching for quite a few days. Reviewed many answers regarding multiple images but no one mentioned about this problem.
I've followed this code https://github.com/websymphony/Rails3-Paperclip-Uploadify.

Paperclip was also uploading the actual image data into the field image in my database. I had to tweak it to save file names in the image_file_name field in my database.
Here is my controller that saves the image from the upload form.
#paperclip replaces spaces with _
formatted_filename = params[:clothe][:image].original_filename
formatted_filename.gsub!(/\s/,'_')
#hook in image processing
#set type of upImg, formUpload (APIUpload, scrapeUpload, mobileUpload)
image = UploadImage.new(formatted_filename, Rails.root.to_s + '/public/products/', #clothe.id)
image.processImage
Here is my model
class Product < ActiveRecord::Base
attr_accessible :description, :price, :title, :image, :image_file_name, :published
has_attached_file :image,
:styles => {
:thumb => "100x100#",
:small => "150x150>",
:medium => "200x200" },
:default_url => '/assets/missin.gif',
:path => Rails.root.to_s + "/public/products/:filename",
:url => "/products/published/:basename.:extension"

Related

Paperclip multiple file content types

I want to be able to upload and validate a particular file type based on different views on the same model which is UploadedFile
This is what I have so far below and I want to be able to use imageable as polymorphic association with other models and validate depending on what controller is processing specific action. For example, I have form that submits images for a particular view then another one for submitting videos.
class UploadedFile < ActiveRecord::Base
belongs_to :imageable, polymorphic: true
has_attached_file :assets
validates_attachment :assets,
:content_type => /^image\/(png|gif|jpeg)/,
:default_url => "/",
:message => "only (png|gif|jpeg) images are allowed and the size cannot exceed 5mb"
:size => { :in => 0..5000.kilobytes }
end
So what I need is if image is submitted, I validate according to image validation_attachment settings then change this to video settings if submitted from the video form.
How would I go about this with Paperclip and Rails 4?

Paperclip dosnt save attachments if password has attr_accessor

My project:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :email,
:password,
:password_confirmation,
:first_name,
:last_name,
:birth_date,
:residence,
:user_role,
:show_email,
:avatar
as_attached_file :avatar,
:default_url => '/images/system/user_avatars/default/default_avatar.png',
:url => "/public/images/system/user_avatars/:id_:style.:extension",
:path => "/public/system/user_avatars/:id_:style.:extension"
def update_profile(user_id, params) #params has :category and :user params
#user = User.find(user_id)
#user.update_attributes(params[:user])
return params[:category]
end
end
So, from my controller i call this method and i get no error. Paperclip shows attachment saved. The database is updated, but the image file is not saved. I have an registration made from scratch, so that's why i have the "attr_accessor :password"
I checked:
Have :multipart => true in form
Have attr_accessible :avatar in user model
Can any one give me some lead, cos i cant figure, why paperclip dosnt save the file.
Set attr_accessible :avatar_file_name as well, and you also need a paperclip.rb initializer:
require "paperclip"
Paperclip.options[:command_path] = "/ImageMagick"
And, of course, have ImageMagick installed.

CanCan gem with nested resources and :find_by

Im using CanCan load_and_authorize_resource helper method for fetching resources and generating authorization, but I have a nested resource like this
load_and_authorize_resource :company
load_and_authorize_resource :accountings, :through => :company, :class => 'Departments::Accounting'
But I need
#accountings be found by another attribute rather that the Departmets::Accounting id and give a value to that attribute, for example
#accountings = #company.find_by_period_id(#period.id)
You can do it with two extra options:
load_and_authorize_resource :accountings,
:through => :company, :class => 'Departments::Accounting',
:find_by => :attr, # :period_id in your example
:id_param => :something # this searches the params hash for
# params[:something], and sends that
# value to .find_by_attr
Check the code for load_and_authorize_resource here.

Issues with Carrierwave in Rails 3.1.0

I'm trying to attach a file "attachment" to my upload model. The attachment field in my db after creation is nil, and links link #upload.attachment.url just redirect to a parent object. Maybe I'm doing something wrong? I haven't used Carrierwave before.
# Model
require 'carrierwave/orm/activerecord'
class Upload < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
end
Went with the basics for for the attachment field
# Form
= form_for #upload, :html => { :multipart => true } do |f|
%br
= f.file_field :attachment
And more basics with the controller:
def create
#upload = Upload.new(params[:upload])
#upload.attachment = params[:file]
if #upload.save
redirect_to #upload
end
end
I'm not getting any errors in my console, but the :attachment string on the student model is always nil.
Thanks!
Why u have added the line
#upload.attachment = params[:file]
remove it. it will work. attachment string is null because there is not params file in the form.

Editing records with SQLite, DataMapper, and Sinatra

I'm in the process of learning Sinatra and DataMapper. To do so, I've been playing with a "customer database" project.
Creating and deleting records is trivial and now I'm working on editing records. So far I've managed to piece together a form in my views and a couple of routes that I thought would edit a record. Here's some code to illustrate my issue:
My edit.erb view: http://gist.github.com/308405
My edit/update routes:
get '/edit/:acct' do
#title = "Edit Client Data"
#client = HE_Backend.get(params[:acct])
erb :edit
end
post '/update/:acct' do
client = HE_Backend.get(params[:acct])
client.attributes = {
:name => params['client']['name'],
:company => params['client']['company'],
:street => params['client']['street'],
:state => params['client']['state'],
:zip => params['client']['zip'],
:phone => params['client']['phone'],
:fax => params['client']['fax'],
:website => params['client']['website'],
:order_date => params['client']['order_date'],
:payment_date => params['client']['payment_date'],
:monthly => params['client']['monthly'],
:setup => params['client']['setup'],
:details => params['client']['details'],
:notes => params['client']['notes'],
:status => params['client']['status'],
}
if client.save
redirect "/show/#{client.acct}"
else
redirect('/list')
end
end
It looks like the "client.save" portion of the route is returning false, because I'm getting redirected to "/list" each time. If I use the #update method rather than #save, DM complains about "dirty records".
Anyone have any ideas as to what I'm doing wrong or can you point me to examples for editing records in SQLite with DataMapper and Sinatra?
Thanks!
This turned out to be a validations issue. If I don't have validations in place and put data types other than what's in my model in those fields, the #save method apparently returns false.

Resources