我正在学习轨道,我正在跟随Mattan Griffel的“One Mont Rails”。'我花了很多时间寻找这个答案,并尝试了一切,但似乎没有任何工作。我正在使用paperclip gem来上传图片,但上传后,图片不会显示。唯一显示的是缺少的图像图标。请有人帮忙。非常感谢。
这是我的github回购链接:github.com/robertguss/omrails
引脚控制器:pins_controller.rb
class PinsController < ApplicationController
respond_to :html, :xml, :json
before_filter :authenticate_user!, except: [:index]
before_action :set_pin, only: [:show, :edit, :update, :destroy]
def index
@pins = Pin.all
respond_with(@pins)
end
def show
respond_with(@pin)
end
def new
@pin = current_user.pins.new
respond_with(@pin)
end
def edit
@pin = current_user.pins.find(params[:id])
end
def create
@pin = current_user.pins.new(pin_params)
@pin.save
respond_with(@pin)
flash[:success] = "Pin was created successfully!"
end
def update
@pin = current_user.pins.find(params[:id])
@pin.update(pin_params)
respond_with(@pin)
flash[:success] = "Pin was updated successfully!"
end
def destroy
@pin = current_user.pins.find(params[:id])
@pin.destroy
respond_with(@pin)
flash[:alert] = "Pin was deleted!"
end
private
def set_pin
@pin = Pin.find(params[:id])
end
def pin_params
params.require(:pin).permit(:description)
end
end
Pins Model = pin.rb
class Pin < ActiveRecord::Base
validates :user_id, presence: true
validates :description, presence: true
has_attached_file :image,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
validates_attachment :image, content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png' 'image/gif'] },
size: { less_than: 5.megabytes }
belongs_to :user
end
答案 0 :(得分:0)
在def pin_params
上,您应该添加图片文件的权限;所以它会是:
def pin_params
params.require(:pin).permit(:description, :image)
end