回形针图像上传商店零并显示缺失图像

时间:2016-08-14 20:52:16

标签: ruby-on-rails ruby paperclip

我正在使用Paperclip上传图像并在针脚中显示。但它显示缺少的图像,并且引脚存储图像属性的空值。我在这里经历了不同的帖子,但仍然无法修复它。

<Pin id: 7, description: "Hello", created_at: "2016-08-14 20:13:10", updated_at: "2016-08-14 20:13:10",user_id: 2, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil>

图钉模型

class Pin < ActiveRecord::Base
    belongs_to :user
    has_attached_file :image
    validates_attachment :image, content_type: { content_type: ["image/jpg",
    "image/jpeg", "image/png", "image/gif"] }
end

引脚控制器

class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]

def index
  @pins = Pin.all
end

def show
end

def new
  @pin = current_user.pins.build
end

def edit
end
def create
   @pin = current_user.pins.build(pin_params )
   respond_to do |format|
     if @pin.save
       format.html { redirect_to @pin, notice: 'Pin was successfully created.' }
       format.json { render :show, status: :created, location: @pin }
     else
       format.html { render :new }
       format.json { render json: @pin.errors, status: :unprocessable_entity }
     end
  end
end

def update
respond_to do |format|
  if @pin.update(pin_params)
    format.html { redirect_to @pin, notice: 'Pin was successfully updated.' }
    format.json { render :show, status: :ok, location: @pin }
  else
    format.html { render :edit }
    format.json { render json: @pin.errors, status: :unprocessable_entity }
  end
 end
end

def destroy
   @pin.destroy
   respond_to do |format|
      format.html { redirect_to pins_url, notice: 'Pin was successfully destroyed.' }
      format.json { head :no_content }
   end
 end

 private
   # Use callbacks to share common setup or constraints between actions.
   def set_pin
     @pin = Pin.find(params[:id])
   end

  def correct_user
     @pin = current_user.pins.find_by(id: params[:id])
     redirect_to pins_path, notice: "Unauthorized to edit" if @pin.nil?
  end

   # Never trust parameters from the scary internet, only allow the white  list through.
  def pin_params
     params.require(:pin).permit(:description, :image)
     end
  end

查看

<%= form_for @pin, html: {multipart: true} do |f| %>
<% if @pin.errors.any? %>
  <div id="error_explanation">
    <h2><%= pluralize(@pin.errors.count, "error") %> prohibited this pin from being saved:</h2>

    <ul>
    <% @pin.errors.full_messages.each do |message| %>
      <li><%= message %></li>
    <% end %>
    </ul>
  </div>
<% end %>
<div class="col-sm-8">
  <div class="form-horizontal">
    <div class="form-group">
      <div class="field">
        <%= f.label :image , class: "col-sm-3 control-label" %>
        <div class="col-sm-5"><%= f.file_field :image , class: "form-control" %>
        </div>
      </div>
    </div>
    <div class="form-group">
      <div class="field">
        <%= f.label :description , class: "col-sm-3 control-label" %>
        <div class="col-sm-5"><%= f.text_field :description , class: "form-control" %>
        </div>
      </div>
    </div>
    <div class="form-group">
      <div class="actions">
        <div class="col-sm-3"></div>
        <div class="col-sm-5">
        <%= f.submit class: "btn btn-danger" %>
        </div>
      </div>
    </div>  
  </div>
  

0 个答案:

没有答案
相关问题