Uploading File without any extarnal gem rails 4 -
After watching the train guide 4 I am trying to upload an image without any gem, but when my button field My image field is empty if it's ready.
Here's my controller:
Class Image Controller & lt; ApplicationController before_action: set_image, only: [: Show, Edit, Upload, Update: Delete] #GET / Images # GET /images.json DNF Index @ iGues = Image. All #GIT / Image / 1 # GET /images/1.json def Show End # GET / images / new def new @image = Image.new end # GET / images / 1 / def Edit Edit end # POST / images # POST /images.json def @image = Image.new (image_params) If the consultation [: image]. Displayed? File = param [: picture] [: picture] file. Open (rail .. rot.joint ('app', 'assets', 'image', file.originalfilename), 'wb'). F | F.write (file.read) to end the response_. Format | If @ image.save format.html {redirect_to @image, notice: 'Image was created successfully.' } Format.json {Render Action: 'Show', Status :: Created, Location: @Ijet} and Format. Html {Action: 'New'} format.json {Render Jason: @image. Errors, Position :: Inappropriate} End End # Patch / Pop / Image / 1 # Patch / Put / Image / 1 JSN DIF Update Reply-To-Do | Format | If @ image.update (image_params) format.html {redirect_to @image, notice: 'Image was updated successfully.' } Format.json {head: no_content} else format.html {render action: 'edit'} format.json {json: @ image.errors, status :: unprocessable_entity} End & # DELETE / images / # DELETE / images / 1.json delete DEF @ image.destroy response_to do | Format | Format.html {redirect_to images_url} format.json {head: no_content} end-of-the-file upload file uploaded upload = params.require (: image) .permit (: image) File.open (Rails.root.join (' Public ',' upload ', uploaded_ORGINAL_filename),' wb '). File | File.write (upload_io.read) Use callback to share common setup or barriers between end-end personal # tasks def set_image @image = Image.find (params [: id]) end # scary anytime from the internet Do not believe in the parameter, only allow white image through the list. Def image_params params.require (: image) .permit (: name) end end
This is my _form.html .erb The file is:
& lt; % = Form_for (@image) do F | | & Gt%; & Lt; Div class = "field" & gt; & Lt;% = f.label: name% & gt; & Lt; Br> & Lt;% = f.text_field: name% & gt; & Lt; / Div & gt; & Lt; Div class = "field" & gt; & Lt;% = f.label: Pic% & gt; & Lt; Br> & Lt;% = f.file_field: PIC% & gt; & Lt; / Div & gt; & Lt; Div class = "verb" & gt; & Lt;% = f.submit% & gt; & Lt; / Div & gt; & Lt;% end% & gt;
My route file:
Imageupload :: Application.routes.draw Do resources: end of images
My schema.rb file:
ActiveRecord :: Schema.define (version: 20140725043842) Create _table "images", force: true do | T | T.string "name" t.string "picture" t.datetime "created_at" t.datetime "updated_at" and end
here my show.html. The ARB file is:
Name: & lt; / Strong> & Lt;% = @ image.name% & gt; & Lt; / P & gt; & Lt; P & gt; & Lt; Strong> Image: & lt; / Strong> & Lt;% = image_tag @ image.picture% & gt; & Lt; / P & gt; & Lt;% = link_to 'Edit', edit_image_path (@image)% & gt; | & Lt;% = link_to 'back', images_path% & gt;
What's wrong ?? Or can you tell me how to upload files without external gem?
Regards, Host
: multipart => true
is exempt.
Add it up
& lt; % = Form_for_image, {},: html = & gt; {: Multipurpose = & gt; True} what? F | & Gt%;
It says that
If you use
form_for
, then it is done automatically - < Strong> "Multipart / Form-Data"
But I'm not sure.
Comments
Post a Comment