Rails 3.1 Thumbnail Image Rotation / Resizing On-the-fly - ruby-on-rails-3.1

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) %>

Related

How to implement "change your profile picture" feature (server side)

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.

carrierwave image not loading into source code

Not sure what I'm doing wrong on this one. I've followed the Rails Cast for Carrierwave but am having a strange bug where the image isn't showing at all - the (HTML) source code is showing the image tag but nothing inside it.
Portfolio Model code:
class Portfolio < ActiveRecord::Base
validates :title, :content, presence: true
mount_uploader :feature_image, FeatureImageUploader
end
Feature Image Uploader code:
class FeatureImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Show.html.haml code:
= #portfolio.title
=image_tag #portfolio.feature_image_url.to_s
=markdown(#portfolio.content).html_safe
And my form code:
.field
= f.label :title
%br
= f.text_field :title
.field
= f.label :date
%br
= f.datetime_select :date
.field
= f.label :content
%br
= f.text_area :content, rows: 10
.field
= f.label :feature_image
= f.file_field :feature_image
.actions
= f.submit
And my HTML source code is showing:
<img src=""/>
I've run my rake tests and everything is fine, no failures. Would someone mind having a look for me, would really appreciate it.
Thank you!
EDIT
This is my pastebin from the Server logs when I add a new portfolio entry - http://pastebin.com/1zNxB975
in views
=image_tag #portfolio.feature_image.url
and controller
portofolios_controller.rb
...
private
def portofolio_params
params.require(:portofolio).permit(:title, :date, :content, :feature_image)
end
not sure about the other params you might have and need, but :feature_image is a must.

How can I create a new layout and have it show up only in certain situations using ruby padrino?

I'm using padrino with my website, and need to override the default layout to use a different layout in certain situations i.e. when a user clicks on a certain thing.
Padrino documentation gives these examples:
SimpleApp.controllers :posts do
# Apply a layout for routes in this controller
# Layout file would be in 'app/views/layouts/posts.haml'
layout :posts
get("/posts") { render :haml, "Uses posts layout" }
end
SimpleApp.controllers :admin do
get :index do
render "admin/index", :layout => :admin
end
get :show, :with => :id do
render "admin/show", :layout => false
end
end

save images using paperclip without view

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)

Paperclip attachment column in database is empty

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

Resources