查看 rails guide 4 guides.rubyonrails.org/v4.0.6/form_helpers.html 后。我正在尝试上传没有任何 gem 的图像,但是当点击创建按钮时,我的图片字段为空。
这是我的控制器:
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 %>
怎么了?或者您可以告诉我如何在没有任何外部 gem 的情况下上传文件。
:multipart => 真
错过了。
像这样加起来
<%= form_for @image, {},:html => {:multipart => true } do |f| %>
它说上传文件
如果你使用
form_for,这是自动完成的—— “multipart/form-data”
没试过。
| 归档时间: | 
 | 
| 查看次数: | 4302 次 | 
| 最近记录: |