【发布时间】:2014-06-19 21:01:25
【问题描述】:
我只是想用回形针上传一些非常简单的图片。我用谷歌搜索了这个问题,似乎其他人的问题比简单的上传要复杂得多。下面是我的模型和控制器。
pin.rb
class Pin < ActiveRecord::Base
belongs_to :user
has_attached_file :image
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
# validates the description
validates :description, presence: true
validates :user_id, presence: true
# validates paperclip
validates_attachment :image, presence: true,
content_type: { content_type: ["image/jpeg", "image/jpg", "image/png", "image/gif"]},
size: { less_than: 5.megabytes }
end
pin_controller.rb
class PinsController < ApplicationController
#before_filer will authentiate users to make sure they are logged in before doing anything with the Pins, with the except of indexing the pins so non-logged on users can also see them
before_filter :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
# associate @pin to the current user's id
@pin = current_user.pins.new
end
# GET /pins/1/edit
def edit
# makes sure no other user can mess without a proper user id
@pin = current_user.pins.find(params[:id])
end
# POST /pins
# POST /pins.json
def create
# associate @pin to current user's id
@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 action: 'show', status: :created, location: @pin }
else
format.html { render action: 'new' }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /pins/1
# PATCH/PUT /pins/1.json
def update
# makes sure no other user can mess without a proper user id
@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 { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @pin.errors, status: :unprocessable_entity }
end
end
end
# DELETE /pins/1
# DELETE /pins/1.json
def destroy
# makes sure no other user can mess without a proper user id
@pin = current_user.pins.find(params[:id])
@pin.destroy
respond_to do |format|
format.html { redirect_to pins_url }
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
以下是我认为我的代码可能有问题的地方。
首先,我跟着一个月的Rails教程,但由于我正在看过时的Rails 3系列,我必须通过研究将他教的所有内容都转换为Rails 4。我可能在我的 pin.rb 控制器中设置了错误的强参数。我像这样在 pin_params 方法中添加了 :image 属性字段
params.require(:pin).permit(:description, :image)
这是使用强参数添加 :image 的正确方法吗?在 rails 3 中,他在 attr_accessible
中添加了 :image第二,在回形针的 README 中,它说我应该运行 which convert 并最终在我的配置文件中输入这一行
Paperclip.options[:command_path] = "/usr/local/bin/"
由于我使用的是 Windows 机器,这是我在寻找答案数小时后最终得到的结果
Paperclip.options[:command_path] = "C:/Program Files/ImageMagick-6.8.9-Q16/"
上面的路径对于windows来说看起来相对正确吗?
【问题讨论】: