I'm using Paperclip to save images from my current local app directory. I have used two models to save images.
business.rb
class Business < ActiveRecord::Base
# Associations
has_attached_file :logo, :styles => {:thumb => "100x100>"}
validates_attachment :logo,
:size => {:in => 1..1500.kilobytes}
end
image.rb
class Image < ActiveRecord::Base
has_attached_file :data, :styles => { :thumb => '100x100>', :medium => '240x240>' }
end
when i try to save image it is not working for me like below.
Business.create(:logo => Image.first.url)
and i have this error
Paperclip::AdapterRegistry::NoHandlerError: No handler found for "/system/images/data/000/000/177/medium/error.png"
while "Image.first.url" give me this path
"/system/images/data/000/000/177/medium/error.png"
How i can do this?
Thanks
You are assigning a String(url of first image) to logo, try reading image content for url and assign it to logo.Try this!
image = open(Image.first.url).read
Business.create(logo: image)
Related
I've got a problem updating the params of a model after adding a picture to the model.
Model is user, I've added a image to the user model with paperclip.
When I upload a picture with paperclip it is stored correctly in filesystem and in Database, but after that I can't change any other param of the user.
Thats the method how I update the attributes
def settings
#usergroups = User.find(session[:user_id]).user_groups
if request.post?
if #user.update_attributes(params[:user])
flash[:notice] = t("flash.saved")
end
end
end
Thats the method for uploading the image
def uploadimage
if request.post?
#user = User.find(params[:userid])
if #user.update_attribute(:image, params[:upload][:image])
flash[:notice] = t("flash.saved")
redirect_to :action => :settings
end
end
end
If the picture is stored in db it looks like this in user table:
Model has this:
has_attached_file :image,
:path => ":rails_root/public/uploads/:id/:filename",
:url => "/uploads/:id/:filename"
UPDATE
validates_attachment :image, :content_type => { :content_type => "image/jpg" }
Picture upload form and other attributes form are two different forms.
Does someone have an Idea whats going wrong?
problem comes from:
validates_attachment :image, :content_type => { :content_type => "image/jpg" }
is there a possibility to only validate if :image attribute is changed?
Im trying to implement a feature on my project, so users can change their profile picture. I'm currently at the point where everything on the front end works (when you hover the mouse over the image it shows camera and when you click it, you can browse through your computer to select an image file) Yet, it doesnt work after that point.
I'm not really sure how to handle server request.
FYI
im using routes.rb and controller .rb files to handle backend.
Also,
should i use iframe, instead of form tag in html?
Thanks!
You don't need an iFrame. Simple form-for ruby helper will help in this case. I will suggest you to use a file-uploading gem like paperclip.
Create a model for your records:
class User < ActiveRecord::Base
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
(I am supposing the model name to be user)
Create a new migration with generator:
rails generate paperclip user avatar
Update your html form as follow:
<%= form_for #user, :url => users_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
Then do following changes in the controller:
def create
#user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:avatar)
end
To display image anyware on the page use the following helper:
<%= image_tag #user.avatar.url %>
This is the easiest way to handle image uploading at server side in rails. You can read more at the paperclip github page.If you don't want to use a gem then then there is a ruby class FileUtils which can help you in achieving same goal.
I want to find out if there is an (easy) way to pre-process images in rails before they get served up through the asset pipeline. Notably i'm looking to rotate and resize the images, hopefully with something like
/thumbs/my_thumb.png?deg=35&height=50
Is paperclip something that would be able to do this? Presumably i would need to create a new model/controller and some kind?
Thanks!
if u want the image to be resized and saved, use
class User < ActiveRecord::Base
attr_accessible :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
and in ur view,
<%= image_tag #user.avatar.url(:medium) %>
<%= image_tag #user.avatar.url(:thumb) %>
I have installed paperclip and imagemagick,and implemented the code to my model and view file.
I have a database column named 'picture' and it is empty no matter if i uploaded a picture or not. the picture acctually exists in the/public/system/decks/pictures/000/000/019/medium folder. i can see all of the uploaded pictures there, but i can't show them cause the database is empty.
My model:
class Deck < ActiveRecord::Base
attr_accessible :picture
has_attached_file :picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }
attr_accessor :picture_file_name
attr_accessor :picture_content_type
attr_accessor :picture_file_size
attr_accessor :picture_updated_at
My view:
<%= form_for #deck,:url => decks_path, :html => { :multipart => true } do |f| %>
<%= f.file_field :slika %>
My migration:
class AddAttachmentPictureToDecks < ActiveRecord::Migration
def change
add_column :decks, :picture, :attachment
end
end
So i get the picture in that folder that i have mentioned before but the picture column in my decks table is empty. I can't get the picture with <%= image_tag #deck.picture.url(:medium) %>, cause my #deck.picture.url holds a /pictures/original/missing.png, my #deck.picture.path and my #deck.picture.picture_file_name also shows nothing.
Thank you.
in the end it looks like i mixed the old syntax and the new one and i couldn't get the fields right in the database. and it looks like the attr_accessors are unneccessary
How can I make padrino-admin page generator produce beautiful custom pages?
By default padrino-admin generates pretty ugly admin pages, totally unmaintainable:
.group
=f.label :title
=f.error_message_on :title
=f.text_field :title, :class => :text_field
%span.description Ex: a simple text
.group
=f.label :name
=f.error_message_on :name
=f.text_field :name, :class => :text_field
%span.description Ex: a simple text
--- more annoyingly redundant frak
.group.navform.wat-cf
=f.submit pat(:save), :class => :button
=f.submit pat(:cancel), :onclick => "window.location='#{url(:pages, :index)}';return false", :class => :button
I wrote a nice AdminFormBuilder < AbstractFormBuilder, connected it with set :default_builder, 'AdminFormBuilder', it generates same admin pages from very short code:
= f.inputs :name, :surname, :email
= f.inputs :password, :password_confirmation, :as => :password
= f.input :role, :as => :select, :options => access_control.roles, :descr => 'a simple text'
= f.submits
Now I want padrino g admin_page to generate more of such pages. What should I do?
There are two ways:
1) Make your custom admin gem copying as base the actual padrino-admin
2) Fork the project (where now we support a new admin based on bootstrap) apply your changes and submit a pull request.
Btw the most interesting file for this job is this: https://github.com/padrino/padrino-framework/blob/master/padrino-admin/lib/padrino-admin/generators/admin_page.rb
Here is one-line patch for padrino-admin gem: https://github.com/ujifgc/padrino-framework/commit/b07399bdfbc15d05682237c64580e77558ac9fce
Now I can place copy of original templates folder from padrino-admin-0.10.5/lib/padrino-admin/generators to vendor/padrino-admin/generators and enjoy my own admin page templates.