我有 gem' paperclip','〜> Rails5中的5.1' 。当我用图像创建配方时出现错误:
Paperclip :: RecipesController中的错误#create
处理缩略图3c39a73424a219191afdb65e04f66d5d20160927-18689-1c0lj5f
时出错指向控制器创建方法。 我已经安装了ImageMagic
这是配方控制器:
class RecipesController < ApplicationController
before_action :find_recipe, only: [:show, :edit, :update, :destroy]
def index
@recipe = Recipe.all.order("created_at DESC")
end
def new
@recipe = Recipe.new
end
def show
end
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
redirect_to @recipe, notice: 'Succesfully created new recipe'
else
render 'new'
end
end
def edit
end
def update
if @recipe.update(recipe_params)
redirect_to @recipe
else
render 'edit'
end
end
def destroy
@recipe.destroy
redirect_to root_path, notice:"Succesfully deleted"
end
private
def recipe_params
params.require(:recipe).permit(:title, :description, :image)
end
def find_recipe
@recipe = Recipe.find(params[:id])
end
端
这是型号:
class Recipe < ApplicationRecord
has_attached_file :image, styles: { medium: "400x400#>" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
端
和_form.html.haml
= simple_form_for @recipe, html: { multipart: true } do |f|
- if @recipe.errors.any?
#errors
%p
= @recipe.errors.count
Prevented this recipe froms saving
%ul
- @recipe.errors.full_messages.each do |msg|
%li= msg
.panel-body
= f.input :title, input_html: { class: 'form-control' }
= f.input :description, input_html: { class: 'form-control' }
= f.input :image, input_html: { class: 'form-control' }
= f.button :submit, class: "btn btn-primary"
和回形针的迁移文件:
class AddAttachmentImageToRecipes < ActiveRecord::Migration
def self.up
change_table :recipes do |t|
t.attachment :image
end
end
def self.down
remove_attachment :recipes, :image
end
end
有人可以帮忙吗?