[英]NoMethodError when using the paperclip gem in rails
I am trying to upload an image using the paperclip gem and imagemagick in rails. 我正在尝试使用回形针gem和imagemagick在rails中上传图像。 I did everything I was supposed to do and I get the following error message when I try to upload.
我做了我应该做的一切,尝试上传时收到以下错误消息。 It is a NoMethodError.
这是一个NoMethodError。 Error message screenshot
错误消息截图
My gem file: 我的宝石文件:
source 'https://rubygems.org'
gem 'rails', '4.2.3'
gem 'sqlite3'
gem 'simple_form', '~> 3.4'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'devise', '~> 4.2', '>= 4.2.1'
gem 'bootstrap-sass', '~> 3.3.4.1'
gem 'coffee-script-source', '1.8.0'
gem 'jbuilder', '~> 2.0'
gem 'paperclip', '~> 5.1'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
My _form.html.erb file: 我的_form.html.erb文件:
<%= simple_form_for @book, :html => { :multipart => true } do |f| %>
<%= select_tag(:category_idd, options_for_select(@categories), :prompt=> "Select a category")%>
<%= f.file_field :book_img%>
<%= f.input :title, label: "Book title" %>
<%= f.input :description, label: "Please describe your book here"%>
<%= f.input :author, label: "Who is the author of this book?" %>
<%= f.button :submit%>
<% end %>
My development.rb file: 我的development.rb文件:
Rails.application.configure do
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.raise_delivery_errors = false
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.assets.debug = true
config.assets.digest = true
config.assets.raise_runtime_errors = true
end
My book.rb file: 我的book.rb文件:
class Book < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_attached_file :book_img, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :book_img, content_type: /\Aimage\/.*\z/
end
My book_controller file: 我的book_controller文件:
class BooksController < ApplicationController
before_action :find_book, only: [:show, :edit, :update, :destroy]
def index
if params[:category].blank?
@books = Book.all.order("created_at DESC")
else
@category_idd = Category.find_by(name: params[:category]).id
@books = Book.where(:category_idd => @category_idd).order("created_at DESC")
end
end
def show
end
def new
@book = current_user.books.build
@categories = Category.all.map{ |c| [c.name, c.id]}
end
def create
@book = current_user.books.build(book_params)
@book.category_idd = params[:category_idd]
if @book.save
redirect_to root_path
else
render 'new'
end
end
def edit
@categories = Category.all.map{ |c| [c.name, c.id]}
end
def update
@book.category_idd = params[:category_idd]
if @book.update(book_params)
redirect_to book_path(@book)
else
render 'edit'
end
end
def destroy
@book.destroy
redirect_to root_path
end
private
def book_params
params.require(:book).permit(:title, :description, :author, :category_idd, :book_img)
end
def find_book
@book = Book.find(params[:id])
end
end
A thing to note, when I remove ":book_image" from the book_params in the book_controller, the error disappears , but I don't think it actually uploads the image since the book params has to have the ":book_img" part in it. 需要注意的是,当我从book_controller中的book_params中删除“:book_image”时,错误消失了,但是我不认为它实际上上传了图像,因为book params中必须包含“:book_img”部分。 Help would be highly appreciated since this is driving me crazy.
非常感谢您的帮助,因为这使我发疯。
EDIT: After removing the :book_img from book_params in the books_controller, I tried submitting a form with an image attached. 编辑:从books_controller中的book_params中删除:book_img之后,我尝试提交带有图像的表单。 I get no error, but when I go to the rails console and see the data in the last uploaded book, they show the data related to "book_img" are all "nil".
我没有收到任何错误,但是当我进入rails控制台并查看上一本上载的书中的数据时,它们显示与“ book_img”相关的数据全部为“ nil”。 So even though the error disappeared, the image was not uploaded.
因此,即使错误消失了,图像也没有上传。
EDIT2: Iceman, I get the following error: Error image screenshot EDIT2:Iceman,我收到以下错误: 错误图像截图
This part is the problem, when the @book
is not saved the form is re-rendered and @categories
is nil
. 这是问题所在,当未保存
@book
时,将重新呈现表单,而@categories
为nil
。 Add a call to fetch @categories
and you should be good to go. 添加一个调用以获取
@categories
,您应该@categories
顺利。
if @book.save
redirect_to root_path
else
@categories = Category.pluck(:name, :id)
render 'new'
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.