繁体   English   中英

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

[英]NoMethodError when using the paperclip gem in rails

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

我的宝石文件:

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,我收到以下错误: 错误图像截图

这是问题所在,当未保存@book时,将重新呈现表单,而@categoriesnil 添加一个调用以获取@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM