Ruby on rails 4 在没有任何外部gem rails的情况下上载文件4

Ruby on rails 4 在没有任何外部gem rails的情况下上载文件4,ruby-on-rails-4,Ruby On Rails 4,查看导轨4后。我试图上传没有任何宝石的图像,但当点击创建按钮,我的图片字段是空的 这是我的控制器: class ImagesController < ApplicationController before_action :set_image, only: [:show, :edit,:upload, :update, :destroy] # GET /images # GET /images.json def index @images = Image.all

查看导轨4后。我试图上传没有任何宝石的图像,但当点击创建按钮,我的图片字段是空的

这是我的控制器:

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
My 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文件:

<%= 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 %>
<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 %>

名称:

图片:

|
怎么了?或者你可以告诉我如何上传文件,没有任何外部宝石

:multipart=>true

他被遗漏了

把它加起来

<%= form_for @image, {},:html => {:multipart => true } do |f| %>
{:multipart=>true}do | f |%>
上面说

如果对使用
form\u,这将自动完成--多部分/表单数据“


未尝试。

图像已上载到/assets/image dir,但当我调用该图像(show.html.erb)时,它未显示。我认为实际问题的答案已得到回答。你可以问一个不同的问题,问你如何使用
code
显示图像亲爱的nithin,需要你的帮助。在
@image.save
之前,设置
@image.picture=“/public/”{file.original_filename}'”
让我们来看看。
<%= form_for @image, {},:html => {:multipart => true } do |f| %>