【问题标题】:NoMethodError when using the paperclip gem in rails在 Rails 中使用回形针 gem 时出现 NoMethodError
【发布时间】:2017-05-06 17:43:03
【问题描述】:

我正在尝试使用 rails 中的回形针 gem 和 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 params 必须具有 ":book_img" 部分在里面。非常感谢您的帮助,因为这让我发疯了。

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

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

【问题讨论】:

  • 能否贴出调用create方法时的日志文件?
  • 嘿 Jaime,我不再收到错误了,因为我添加了冰人告诉我添加的代码(我们下面的答案),它使错误消失了,但实际上并没有上传图片。现在,我已经将它设置为当我点击“添加书籍”时,它会带我到 root_path(主页),如果这本书被保存,如果没有,它将呈现“新”。当我添加您的代码时,它消除了错误,但它继续呈现“新”,因为带有附加图像的书无法保存。但是,如果我创建的书没有附加图片,它会保存这本书并将我带到主页
  • 但是 Rails 应该仍然保存日志。如果您使用rails server 运行应用程序,您应该会在终端窗口中看到日志。如果您以其他方式运行它,您应该在项目文件夹log/environment.log 中找到日志。您正在寻找的答案可能就在那里
  • @JaimeRodas 所以问题似乎是“内容类型欺骗:文件名 scsc.png(来自标头的图像/png,[#<:type: image>] 来自扩展),内容从文件命令中发现的类型:。请参阅文档以允许此组合。

标签: ruby-on-rails imagemagick paperclip


【解决方案1】:

这部分是问题,当@book没有保存时​​,表单被重新渲染,@categoriesnil。添加调用以获取@categories,您应该一切顺利。

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

【讨论】:

  • 嘿伙计,谢谢你。它使错误消失,但实际上并没有上传图像。现在,我已经将它设置为当我点击“添加书籍”时,它会带我到 root_path(主页),如果这本书被保存,如果没有,它将呈现“新”。当我添加您的代码时,它消除了错误,但它继续呈现“新”,因为带有附加图像的书无法保存。但是,如果我创建的书没有附加图片,它会保存这本书并将我带到主页。
  • 尝试save!获取异常,然后你会看到你的错误。
  • 嗯,是的,这是一个完全不同的问题,与您尝试上传的文件有关。我建议您为此提出一个新问题。
  • 是的,先生。会做
猜你喜欢
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多