Rails validation for image link - ruby-on-rails

i am creating a rails application and i wanna user to be able to enter link for his/her profile picture from Facebook or any external site .
how to be sure this link is for image??
how to create a validation for that ??

Use this validation in model.
validates_format_of :image, :with => %r{\.(png|jpg|jpeg)$}i, :message => "Use a real image"
Please be aware that this would only validate that the filename would end in a specific string.
Use fastimage to get type and size of images by quickly fetching.
eg.
FastImage.type("http://stephensykes.com/images/pngimage")
returns .png

Related

two fields for one file

In my form I have field for file uploading which is done with default rails methods, looking like this(I'm using simple_form here, to be clear):
= f.input :spec_url, as: :file
It generates nice and easily field with which user can select any file from his hard drive. But i would like to give users opportunity to simply type this information. since this is field for URL, it should be string field, possible to just fill in, WITH POSSIBILITY to upload something.
How can I achieve this? Some gems maybe?
You will need to have two fields (a string and a file):
= f.input :spec_url, as: :string, id: 'url-field'
= f.input :spec_file, as: :file, id: 'file-field'
Add some javascript (I'm using CoffeeScript) which will set the file name on the spec_url field:
$ ->
$('#file-field').on 'change', (e) =>
$('#url-field').val($(e.target).val())
And then you can do some conditional statements in your Ruby code:
if params[:object][:spec_file]
# Upload file
object.spec_url = params[:object][:spec_file].original_filename
else
# Open URL
object.spec_url = params[:object][:spec_url]
end
Add attr_accessor :spec_file to your model:
attr_accessor :spec_file
This is just a rough idea since I'm not sure what the context is. You might need to modify and style some of the code.
Update
You might also find this question helpful: Rails: upload a file OR store a url

Carrierwave - show thumbnail in ActiveAdmin after upload with remove/delete link

I use ActiveAdmin to upload featured images to posts. It works but after I save the post, and click on edit, the image field is empty, although the image is still attached to it.
What I want to achieve is after upload I would like to show the thumbnail of the image below the image upload field, with an option to remove/delete the image (preferably without touching the save button).
I also use the activeadmin-wysihtml5 gem along with the activeadmin-dragonfly gem which handles image uploads when I want to insert images in the body of the post through the editor.
These gems also created an Assets link in ActiveAdmin, where I can see and handle all the uploaded images. Is it possible to do this with Carrierwave? Or maybe make Carrierwave use the existing assets and upload there?
Best Wishes,
Matt
The answer to this is featured on ActiveAdmin Wiki as Showing an uploaded image in the form
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => f.object.cover_page.present? \
? f.template.image_tag(f.object.cover_page.url(:thumb))
: f.template.content_tag(:span, "no cover page yet")
f.input :cover_page_cache, :as => :hidden
end

How do I post an image using Koala gem?

I'm using the Koala gem to post images to the Facebook wall with the put_object method. The problem I'm having is that the images show up as thumbnails on the wall (with a caption) instead of the larger image that I'm expecting. When you click on the thumbnail, I'm expecting the photo to pop open in Facebook's photo viewer, but it simply directs me to the URL of the image I specified. This seems more like the behavior I'd expect when posting a link to the wall. I've also tried using post_picture with no success. Here is a screenshot of what I'm expecting: http://cl.ly/image/472a2H333z13
My stream permissions include the following:
read_stream,offline_access,manage_pages,publish_stream,create_event,rsvp_event,sms,user_photos,friends_about_me,friends_status,friends_activities,friends_likes,friends_interests,read_insights,friends_photos
The following code works but not as expected - here is a screenshot of how an image posted in using put_object appears on the timeline: http://cl.ly/image/1x3X1K0k2l2D
put_object('me', 'feed', :message => 'this is the message', :name => 'this is the caption', :picture => 'http://www.socialvolt.com/img/producttour/listen.png', :source => 'http://www.socialvolt.com/img/producttour/ssPublish.jpg')
This code doesn't work at all:
put_picture('me', 'feed', :message => 'this is the message', :name => 'this is the caption', :picture => 'http://www.socialvolt.com/img/producttour/listen.png', :source => 'http://www.socialvolt.com/img/producttour/ssPublish.jpg')
I also can't find any difference between :picture and :source.
Can anyone help me figure out what I'm doing wrong?
Here is the code.
graph = Koala::Facebook::API.new(cookies[:acess_token])
begin
graph.put_picture(
"https://dl.dropboxusercontent.com/u/71317935/banners/coy_copper.jpg",
"image/png",
{:message => "introduces Some information"}
)
rescue Exception => e
render :text => e.message and return
end
Off the top of my head, you may need a larger photo. More specifically, I think the width needs to be at least 480px.
The images in your code seem to be less than 480px:
http://www.socialvolt.com/img/producttour/listen.png
http://www.socialvolt.com/img/producttour/ssPublish.jpg
This comes from experience with User Generated Photos. You're not really using Open Graph Actions, so I have no idea if Facebook applies the same rules for this to generic photo uploads.

How to use the same form partial for registration and editing in rails

I have a site on PHP which I want to convert to Rails. I use md5 for password storage and _form partial for registration and edit users data. I made login and password fields required, thats work perfect for registration but when user try to edit his data, something like login, password field again need to fill (password field has password type and stay blank for editing). How I can use those form and solve problem with password enter in edit mode ?
In your model, add a validation on your password fields only if they've been filled in, in other words, on update, the fields can be blank. If they are blank, then they will not be validated or changed.
validates :password,
:confirmation => true,
:length => {:within => 8..20},
:allow_blank => true,
:on => :update
You may also want to add a note on the form, the password fields are only required if you are changing your password or your email address.
You can check object.new_record? (as Mr Yoshiji mentions) to determine if it's a new record (no id yet) or an existing record to change your form hints.

Rails - HowTo - Handle Users w/o Images

I just installed paper_clip to allow user's to upload a profile pic which can be displayed easily with:
<%= image_tag #user.profile_pic.url %>
What I'm curious about now is how to handle user's that don't have a profile_pic uploaded.. How to show a standard site user image (generic)
Should I make a helper?
something like showProfilePic(#user, size)
And then use that helper to show the right size image, either the user's uploaded photo or a generic site profile pic?
thanks. Any existing helpers out there?
You can use the default_url and default_style options to set this. The default default_url is /:attachment/:style/missing.png, so you could drop in missing.png for each style and have that be your generic profile pic. If you want to customize, though...
class User < ActiveRecord::Base
has_attached_file :profile_pic,
:default_style => :thumbnail,
:default_url => '/path/to/:style_default.jpg'
end

Resources