这是我的控制器
class PinsController < ApplicationController
before_action :authenticate_user!, except: [:index]
before_action :set_pin, only: [:show, :edit, :update, :destroy]
# GET /pins
# GET /pins.json
def index
@pins = Pin.all
end
# GET /pins/1
# GET /pins/1.json
def show
end
# GET /pins/new
def new
@pin = current_user.pins.new
end
# GET /pins/1/edit
def edit
@pin = current_user.pins.find(params[:id])
end
# POST /pins
# POST /pins.json
def create
@pin = current_user.pins.new(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
# PATCH/PUT /pins/1
# PATCH/PUT /pins/1.json
def update
@pin = current_user.pins.find(params[:id])
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
# DELETE /pins/1
# DELETE /pins/1.json
def destroy
@pin = current_user.pins.find(params[:id])
@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
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image)
end
end
这是我的模型
class Pin < ActiveRecord::Base
attr_accessible :description, :image
has_attached_file :image, styles: { medium: "320x240>"}
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] },
size: { in: 0..5.megabytes }
belongs_to :user
end
当我尝试上传图像时,我的服务器延迟了3分钟,控制台变得疯狂,在这3分钟之后,我得到这个错误消息-"有内容不是他们报告的内容"。
我刚把模型改成了这个
class Pin < ActiveRecord::Base
attr_accessible :description, :image
has_attached_file :image, styles: { medium: "320x240>"}
validates :description, presence: true
validates :user_id, presence: true
validates_attachment :image, presence: true,
# content_type: { content_type: ["image/jpeg", "image/jpg", "image/gif", "image/png"] },
size: { in: 0..5.megabytes }
validates_attachment_content_type :image, :content_type => /Aimage/
belongs_to :user
end