上传文件没有任何外部宝石轨道4

时间:2014-07-27 17:05:24

标签: ruby-on-rails-4

查看导轨4 guides.rubyonrails.org/v4.0.6/form_helpers.html后。我试图上传没有任何宝石的图像,但是当点击创建按钮时,我的图片字段是空的。

这是我的控制器:

class ImagesController < 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

这是我的_form.html.erb文件:

<%= form_for(@image) do |f| %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :picture %><br>
    <%= f.file_field :picture %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

我的路线档案:

Imageupload::Application.routes.draw do
  resources :images
end

我的schema.rb文件:

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

这是我的show.html.erb文件:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @image.name %>
</p>

<p>
  <strong>Picture:</strong>
  <%= image_tag @image.picture %>
</p>

<%= link_to 'Edit', edit_image_path(@image) %> |
<%= link_to 'Back', images_path %>

出了什么问题?或者你可以告诉我如何在没有任何外部宝石的情况下上传文件。

1 个答案:

答案 0 :(得分:3)

  

:multipart =&gt;真的

错过了。

一样添加它
<%= form_for @image, {},:html => {:multipart => true } do |f| %>

Uploading Files它说

  

如果您使用form_for,则会自动执行此操作 - “multipart / form-data”

没试过。