# db/migrate/20101004063749_create_photos.rb | |
class CreatePhotos < ActiveRecord::Migration | |
def self.up | |
create_table :photos do |t| | |
t.string :name, :null => false | |
t.binary :data, :null => false | |
t.string :filename | |
t.string :mime_type | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :photos | |
end | |
end |
# app/views/photos/new.html.erb | |
<%= form_for(@photo, :html => {:multipart => true}) do |f| %> | |
<div class="field"> | |
<%= f.label :name %> | |
<%= f.text_field :name %> | |
</div> | |
<div class="field"> | |
<%= f.label :data %> | |
<%= f.file_field :data %> | |
</div> | |
<div class="actions"> | |
<%= f.submit "Upload" %> | |
</div> | |
<% end %> |
# app/controllers/photos_controller.rb | |
class PhotosController < ApplicationController | |
def index | |
@photos = Photo.all | |
end | |
def show | |
@photo = Photo.find(params[:id]) | |
end | |
def new | |
@photo = Photo.new | |
end | |
def create | |
# build a photo and pass it into a block to set other attributes | |
@photo = Photo.new(params[:photo]) do |t| | |
if params[:photo][:data] | |
t.data = params[:photo][:data].read | |
t.filename = params[:photo][:data].original_filename | |
t.mime_type = params[:photo][:data].content_type | |
end | |
end | |
# normal save | |
if @photo.save | |
redirect_to(@photo, :notice => 'Photo was successfully created.') | |
else | |
render :action => "new" | |
end | |
end | |
def destroy | |
@photo = Photo.find(params[:id]) | |
@photo.destroy | |
redirect_to(photos_url) | |
end | |
end |
# app/controllers/photos_controller.rb | |
class PhotosController < ApplicationController | |
# ... | |
def serve | |
@photo = Photo.find(params[:id]) | |
send_data(@photo.data, :type => @photo.mime_type, :filename => "#{@photo.name}.jpg", :disposition => "inline") | |
end | |
end |
<%= image_tag serve_photo_path(@photo) %> |
This comment has been minimized.
This comment has been minimized.
It depends on how you are sending the email. Whether you're using action mailer or some other gem, there is a provision to specify the attachments. You can use the same send_data mentioned above in the "serve" action. |
This comment has been minimized.
This comment has been minimized.
Will this work with rails 4? |
This comment has been minimized.
This comment has been minimized.
@Leo-G Not as written. The only thing you'd have to add is a function (if you want) or the like to sanitize the parameters according to rails 4 strong params. Read more here: http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters |
This comment has been minimized.
This comment has been minimized.
Does this have a corresponding model? If not, what's the benefit of omitting that? |
This comment has been minimized.
This comment has been minimized.
I got an error: ActiveModel::ForbiddenAttributesError in PhotosController#create |
This comment has been minimized.
This comment has been minimized.
as Senjai was saying, you have to sanitize the data for rails 4 strong params, e.g., in your controller, private then call photo_params in your new actions |
This comment has been minimized.
This comment has been minimized.
Great tutorial, thanks. I'm having an issue when the user wants to edit the file uploaded. I tried like below in update: |
This comment has been minimized.
how to send these images as mail attachment?plz help..