Image file uploader plugin for Rails 3.2 that can store to Database - ruby-on-rails-3

I'm looking for Rails plugin that can store image file to database.
I've just try "paperclip" and "carrierwave".
They are good plugin.
But they normaly can store to local file system or Amazon S3, fog.
I want to store image file to database.
Please tell me which gem can accomplish it.
thank you.

Here is an example how to you can store an image in binary field (raw_file)
form:
<%= form_for #user do |f| %>
<%= f.file_field :avatar %>
<% end %>
controller:
def create
#user = User.new(params[:user])
# store uploaded avatar as blob
#user.raw_file = params[:user][:avatar].read
#user.save
end
Use RMagick or mini_magick to convert image from blob.

Related

I want to update file to an existing rails record with carrierwave

I have a form where i am asking a user to upload a file.This file to be uploaded is supposed to be saved to a existing record. When a user upload this file i don't want it to be saved right away(because it will create a new record), i want to access the parameters in the controller and update image attribute to the existing record.Currently when i access the params and update_attribute, it shows like it did update but when i look in the database , image attribute is still nil. It works fine when i save directly as new record but it won't let me update manually,can somebody help me?
Thank you in advance
Form
<%= form_tag url_for(:controller=>"deal",:action=>"create_deal"),:method=>'get',:html=>{:multipart=>true} do %>
<%= file_field_tag :image, :value=> params[:image] %>
<%= submit_tag "upload" %>
<%end%>
Controller
def create_deal
#attachment = params[:image]
e_record = Deal.find(:last) #get existing record
if e_record.update_attribute(:image, #attachment)
redirect_to :action=>"profile"
end
end

Link to download a file in rails

I want to send an email with some links to download from my s3 bucket. I have the url of the file stored in a table. How can I use the link_to helper?
<%= link_to "Download File x", #profile.document %>
Because I suppose that #profile.document is a full qualified url, you don't need link_to helper, simply use:
doc`.
Use link_to when you have a resource instance (like Profile probably) that is defined in the config/routes.rb file. For example:
resources :profile
then when you use
<%= link_to 'download', #profile %>
you will have
http://test.com/profiles/34234

Ruby on Rails - image_tag links not working in posts

trying to make link to my recent uploaded image
<%= link_to (image_tag (post.image_url(:thumb))), post.image.url(:original), :class => 'postimage' %>
how ever it's not working, at all...
<% #post.image do |image| %>
<%= link_to (image_tag (post.image_url(:thumb))), post.image.url(:original), :class => 'postimage' %>
<% end %>
the fun part is that
<%= #post.image %>
works. but only shows /uploads/post/image/3/eKoh3.jpg
full code here https://gist.github.com/4332533
This line looks wrong to me:
<% #post.image do |image| %>
(btw your gist actually says #post.image.each do |image|, which I'm assuming is what you meant to do above)
If you are mounting an uploader on your Post model's :image attribute, then this makes no sense. A mounted uploader allows you to upload one image, and you can't iterate over it using each.
I'm not sure what you're trying to do. Are you trying to iterate over all the versions? Try post.image.versions.each
Are you trying to upload multiple images? Carrierwave can't help you with that directly. You'll need to create a new model, Image, and mount your uploader there. Your Post will need a line like
has_many :images
And your Image model will need to belong_to :post. You'll also need to figure out how to upload and manage images in that new table.

Using Devise, I don't see notices like "you've signed in" when i log into my app

I'm building a basic app with Rails 3.2 and Devise 2.0. I've create a User devise model and a Projects model. In my routes.rb files I have
root :to => 'projects#index'
I can sign up and sign in at Get users/sign_up and Get users/sign_in, respectively, but when it redirects to projects#index, I don't see a notice at the top that says "You've signed in successfully. Which file do I need to check to fix this?
You need to add flash messages showing to the file /projects/index.html.erb
for example this way:
<% flash.each do |key, value| %>
<%= content_tag(:div, value, :class => "flash #{key}") %>
<% end %>

Rails 3 - routes for admin section

I am building my first admin section in Rails and I am struggling with routing problems.
My routes.rb looks like this:
get "admin/menuh"
get 'admin/welcome'
namespace :admin do
resources :users
resources :menuh
resources :menuv
resources :welcome
end
And my views structure looks like views/admin/users/files. If I will set to url address of browser the url localhost:3000/admin/users/new, so I will get the error message No route matches {:controller=>"users"} (it's in the file views/admin/users/_form.html.erb - this file is generated by scaffold)... so I would like to ask you - where is the problem? Is here anything important, what I am disregarding?
You've set up your form_for like this, I reckon:
<%= form_for #user do |f| %>
Because the route is in a namespace, you need to tell the form that also:
<%= form_for [:admin, #user] do |f| %>
That should help you fix that issue.