程序代写代做代考 database WDE-Slides-10.pptx

WDE-Slides-10.pptx

Web System Development with
Ruby on Rails

Day 10(1/Dec/2016)

File uploading and Image Display

Today’s Theme
p  Upload image files to the database, and

let Chirps store the image file.

Design Concept of
Image (Figure) Attachment
l  Table Name: figures
l  Model name : figure
l  Relationship : (memos : figures) => 1 to many

l  One ’memo’ can have many images (figures)
l  One picture belongs to only one memos
l  Memos which do not have figures have no problem

without having any figures with it.

l  Figures has ’memo_id’ field for relation
information.

the last
year design

Design Concept of Image Attachment
l  Add field columns to Chirps table, which can

contain image storage/display information.
l  Abandon the design of last year, which allows

the one (chirp/tweet) to many (images)
relationship by adding the new table of
attachment.

Photo display and metadata
To upload and display photo, add photo

method, and add metadata information to
the chirps_controller.rb

def photo
@chirp = Chirp.find( params[:id] )
send_data @chirp.photo, :filename => @chirp.file_name,

:type => @chirp.file_type
end

chirps_controller.rb

Modify create method
# POST /chirps
# POST /chirps.json
def create
if params[:chirp][:photo]
@file = params[:chirp][:photo]
@chirp = Chirp.new(
:user_id => current_user.id,
:chirp => params[:chirp][:chirp],
:photo => @file.read,
:file_name => @file.original_filename,
:file_type => @file.content_type
)
respond_to do |format|
if @chirp.save
format.html { redirect_to @chirp, notice: ‘Chirp was successfully created.’ }
format.json { render :show, status: :created, location: @chirp }
else
format.html { render :new }
format.json { render json: @chirp.errors, status: :unprocessable_entity }
end
end
else
@chirp = Chirp.new(chirp_params)
@chirp.user_id = current_user.id
respond_to do |format|
if @chirp.save
format.html { redirect_to @chirp, notice: ‘Chirp was successfully created.’ }
format.json { render :show, status: :created, location: @chirp }
else
format.html { render :new }
format.json { render json: @chirp.errors, status: :unprocessable_entity }
end
end
end
end

Original Part

chirps_controller.rb
Retrieve the file information such as file

name, file type, and the binary image of
the photo contents, from :photo

Confirm that :photo is in the params list in

the controller method.

chirps_controller # create (P1/2)

chirps_controller # create (P2/2)

To accept :photo in chirp
In order to accept image file to chirp/tweet,

we have changed _form.html.erb.

Leave a Reply

Your email address will not be published. Required fields are marked *