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

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.

Related

problems after paperclip adding a image to controller

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?

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

Rails 3.1 Thumbnail Image Rotation / Resizing On-the-fly

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

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

Multiple Contact Forms - Rails 3

I am relative newbie to all this, so sorry if this sounds mad!
I have used this tutorial: http://www.railsmine.net/2010/03/rails-3-action-mailer-example.html
And I have a new contact form working great.
The controller is at app/controllers/support_controller.rb
class SupportsController < ApplicationController
def new
# id is required to deal with form
#support = Support.new(:id => 1)
end
def create
#support = Support.new(params[:support])
if #support.save
redirect_to('/', :notice => "Support was successfully sent.")
else
flash[:alert] = "You must fill all fields."
render 'new'
end
end
end
And the model at /app/models/support.rb
class Support
include ActiveModel::Validations
validates_presence_of :email, :sender_name, :support_type, :content
# to deal with form, you must have an id attribute
attr_accessor :id, :email, :sender_name, :support_type, :content
def initialize(attributes = {})
attributes.each do |key, value|
self.send("#{key}=", value)
end
#attributes = attributes
end
def read_attribute_for_validation(key)
#attributes[key]
end
def to_key
end
def save
if self.valid?
Notifier.support_notification(self).deliver!
return true
end
return false
end
end
The views however only work in views/supports/new.html.rb (rendered - views/supports/_form.html.erb)
So I can call the Model / Controller from localhost:3000/support/new but if I try and render the same form in another view from the root directory e.g. app/view/contact.html.erb I get:
undefined method `model_name' for NilClass:Class
I think this is because it is calling the support model away from the supports directory.
Do I have to create an instance on #support so it can be called? If so what is the best way of doing that? I think I am nearly there. I just want the contact form on multiple pages not just in suppport/new
Thanks
Charlie
Yes, you would need to create a #support variable in each action you wish to render your form.
Another option would be to refactor the form to take a parameter, that way you're a bit more flexible. For example, from your view:
<%= render :partial => "supports/form", :locals => {:support => #support} %>
Now, instead of referring to #support in your _form.html.erb, you'd refer to simply support as it's a local_assign.
Yet another option would be to refactor the form a little further, and worry about creating the actual form tag outside of the partial.
Such as:
app/views/supports/new.html.erb
<%= form_for #support do |form| %>
<%= render :partial => "suppports/form", :object => form %>
<% end %>
app/views/supports/_form.html.erb
<%= form.text_field :foo %>
<%= form.text_field :bar %>
...
In this case, when you render a partial with the object option, you will get a local variable in your partial with the same name as the partial. You maintain a little bit more flexibility in the path of your form, but can still render the meat of what a Support object is inside of the form while remaining consistent across your app.
To clarify, you could use this somewhere else by doing something like:
app/views/foos/_create_foo_support.html.erb
<%= form_for #foo.support do |form| %>
<%= render :partial => "supports/form", :object => form %>
<% end %>
You have to pass #support object wherever you use your contact form. It's working in SupportsController#new because you initialize the variable there. In all other places where you want to use the form, you'll have to do the same.

Resources