在rails中使用paperclip gem时的NoMethodError

时间:2017-05-06 17:43:04

标签: ruby-on-rails imagemagick paperclip

我正在尝试使用paperclip gem和imagemagick在rails中上传图像。我做了我应该做的一切,当我尝试上传时收到以下错误消息。这是一个NoMethodError。 Error message screenshot

我的宝石文件:

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:冰人,我收到以下错误:Error image screenshot

1 个答案:

答案 0 :(得分:0)

这部分是问题,当未保存@book时,表单会重新呈现,@categoriesnil。添加对fetch @categories的调用,您应该好好去。

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