-
-
Save macek/610596 to your computer and use it in GitHub Desktop.
| # 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 |
| # rails3 | |
| resources :photos do | |
| get "serve", :on => :member | |
| end | |
| # rails 2.x | |
| resources :photos, :member => {:serve => :get} |
| <%= image_tag serve_photo_path(@photo) %> |
Does this have a corresponding model? If not, what's the benefit of omitting that?
I got an error:
ActiveModel::ForbiddenAttributesError in PhotosController#create
:(
I am new in Rails
as Senjai was saying, you have to sanitize the data for rails 4 strong params, e.g., in your controller,
private
def photo_params
params(:photo).permit(:parameter_name_1,:parameter_name2)
end
then call photo_params in your new actions
Great tutorial, thanks. I'm having an issue when the user wants to edit the file uploaded. I tried like below in update:
def update
if params[:record_revision][:filename]
@record_revision = RecordRevision.find(params[:id]) do |t|
t.file_contents = params[:record_revision][:filename].read
t.filename = params[:record_revision][:filename].original_filename
t.content_type = params[:record_revision][:filename].content_type
end
end
but still have "... undefined method `name' for nil:NilClass ..."
could you help on that?
thanks
@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