title: Uploading file without any external gem rails 4
body: <p>After viewing rails guide 4 <a href="http://guides.rubyonrails.org/v4.0.6/form_helpers.html" rel="nofollow noreferrer">guides.rubyonrails.org/v4.0.6/form_helpers.html</a>. I am trying to upload image without any gem, but when hit create button, my picture field is empty.</p>
  
  <p>Here is my Controller :</p>
  
  <pre><code>class ImagesController &lt; ApplicationController
    before_action :set_image, only: [:show, :edit,:upload, :update, :destroy]
  
    # GET /images
    # GET /images.json
    def index
      @images = Image.all
    end
  
    # GET /images/1
    # GET /images/1.json
    def show
    end
  
    # GET /images/new
    def new
      @image = Image.new
    end
  
    # GET /images/1/edit
    def edit
    end
  
    # POST /images
    # POST /images.json
    def create
      @image = Image.new(image_params)
      if params[:image].present?
        file = params[:image][:picture]
        File.open(Rails.root.join('app','assets', 'images', file.original_filename), 'wb') do |f|
          f.write(file.read)
        end
      end
  
      respond_to do |format|
        if @image.save
          format.html { redirect_to @image, notice: 'Image was successfully created.' }
          format.json { render action: 'show', status: :created, location: @image }
        else
          format.html { render action: 'new' }
          format.json { render json: @image.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /images/1
    # PATCH/PUT /images/1.json
    def update
      respond_to do |format|
        if @image.update(image_params)
          format.html { redirect_to @image, notice: 'Image was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: 'edit' }
          format.json { render json: @image.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /images/1
    # DELETE /images/1.json
    def destroy
      @image.destroy
      respond_to do |format|
        format.html { redirect_to images_url }
        format.json { head :no_content }
      end
    end
  
  #file upload
  
    def upload
      uploaded_io = params.require(:image).permit(:picture)
      File.open(Rails.root.join('public','uploads',uploaded_io.original_filename), 'wb') do |file|
        file.write(uploaded_io.read)
      end
    end
  
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_image
        @image = Image.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def image_params
        params.require(:image).permit(:name)
      end
  end
  </code></pre>
  
  <p>Here is my _form.html.erb file:</p>
  
  <pre><code>&lt;%= form_for(@image) do |f| %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :picture %&gt;&lt;br&gt;
      &lt;%= f.file_field :picture %&gt;
    &lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My route file:</p>
  
  <pre><code>Imageupload::Application.routes.draw do
    resources :images
  end
  </code></pre>
  
  <p>My schema.rb file:</p>
  
  <pre><code>ActiveRecord::Schema.define(version: 20140725043842) do
  
    create_table "images", force: true do |t|
      t.string   "name"
      t.string   "picture"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  
  end
  </code></pre>
  
  <p>Here is my show.html.erb file:</p>
  
  <pre><code>&lt;p id="notice"&gt;&lt;%= notice %&gt;&lt;/p&gt;
  
  &lt;p&gt;
    &lt;strong&gt;Name:&lt;/strong&gt;
    &lt;%= @image.name %&gt;
  &lt;/p&gt;
  
  &lt;p&gt;
    &lt;strong&gt;Picture:&lt;/strong&gt;
    &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;
  </code></pre>
  
  <p>What's wrong? or you can tell me how can I upload file without any external gem.</p>