我正嘗試使用回形針中的回形針gem和imagemagick上傳圖像。我做了所有我應該做的事情,當我嘗試上傳時,出現以下錯誤消息。這是一個NoMethodError。 Error message screenshot在軌道中使用回形針寶石時出現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_image」從book_controller的book_params,錯誤消失,但我不認爲它實際上自從上傳圖像本書的參數必須包含「:book_img」部分。幫助將不勝感激,因爲這讓我瘋狂。
編輯: 從books_controller中的book_params中刪除:book_img後,我嘗試提交附帶圖像的表單。我沒有遇到任何錯誤,但是當我轉到rails控制檯並查看上一本上傳圖書中的數據時,它們顯示與「book_img」相關的數據全部爲「無」。所以即使錯誤消失了,圖像也沒有上傳。
EDIT2:冰人,我得到以下錯誤:Error image screenshot
你可以發佈'create'方法被調用的日誌文件嗎? –
嘿Jaime,我不再犯錯,因爲我添加了代碼冰人告訴我添加(下面的答案),它使錯誤消失,但它並沒有真正上傳圖像。現在,我已經設置了這種方式,當我點擊「添加書籍」時,它會將我帶到root_path(主頁),如果書籍已保存,則不會顯示「新的」。當我添加你的代碼時,它刪除了錯誤,但它保持渲染「新」,因爲附帶圖像的書不能被保存。但是,如果我創建了一本沒有附加圖片的書,它將保存圖書並將我帶到主頁 – Dinukaperera
但Rails仍應該保存日誌。如果您使用'rails server'運行應用程序,則應該在終端窗口中看到日誌。如果你以其他方式運行它,你應該在項目文件夾'log/environment.log'中找到日誌。你正在尋找的答案可能會在那裏 –