在 rails 中使用回形针 gem 时出现 NoMethodError

NoMethodError when using the paperclip gem in rails

我正在尝试使用回形针 gem 和 rails 中的 imagemagick 上传图像。我做了我应该做的所有事情,但在我尝试上传时收到以下错误消息。这是一个 NoMethodError。 Error message screenshot

我的 gem 文件:

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]

我的 _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 %>

我的 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

我的 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

我的 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

需要注意的一点是,当我从 book_controller 中的 book_params 中删除“:book_image”时,错误消失了,但我认为它实际上并没有上传图片,因为书中的参数必须包含“:book_img”部分。非常感谢您的帮助,因为这让我发疯。

编辑: 从 books_controller 中的 book_params 中删除 :book_img 后,我尝试提交附有图片的表单。我没有收到任何错误,但是当我转到 rails 控制台并查看上次上传的书籍中的数据时,它们显示与 "book_img" 相关的数据都是 "nil"。所以即使错误消失了,图片也没有上传。

EDIT2:Iceman,我收到以下错误:Error image screenshot

这部分是问题所在,当 @book 未保存时重新呈现表单,@categoriesnil。添加调用以获取 @categories,您应该可以开始了。

if @book.save
  redirect_to root_path
else
  @categories = Category.pluck(:name, :id)
  render 'new'
end