【问题标题】:Rails paperclip error undefined method `before' for false:FalseClass when attaching image to model将图像附加到模型时,Rails 回形针错误未定义方法“之前”为 false:FalseClass
【发布时间】:2018-04-25 07:24:41
【问题描述】:

我有一个名为 Question 的模型,我想使用 Paperclip (5.0.0) 将文件附加到(图表)。保存模型时出现此错误:

undefined method `before' for false:FalseClass

我的模特:

class Question < ActiveRecord::Base
  belongs_to :subject
  belongs_to :category

  has_attached_file :diagram
  validates_attachment_content_type :diagram, :content_type => ["image/jpg", 
    "image/jpeg", "image/png"], if: :hasdiagram

end

我的控制器和发生错误的行

class QuestionsController < ApplicationController

  def new
    @question = Question.new
    render :new
  end


  def create
    @question = Question.new(question_params)  #ERROR OCCURS HERE
    if @question.save
      flash[:success] = "New question created"
      redirect_to admin_portal_path
    else
      render :new
    end
  end

 private

   def question_params
     params.require(:question).permit(:question, :option1, :option2, 
     :option3, :option4, :answer, :category_id, :subject_id, :diagram)
   end

end

整个错误:

NoMethodError (undefined method `before' for false:FalseClass):
app/controllers/questions_controller.rb:12:in `create'

我的表格:

<h1 align="center">Add a new question</h1>
  <div class="col-md-4 offset-md-4">
    <%= render 'shared/errors', object: @question %>
    <%= form_for @question, :url => { :controller => 'questions', :action => 'create'}, method: :post do |f| %>

      <div class="form-group" id="diagram-input">
        <%= f.file_field :diagram %>
      </div>

      <%= f.submit "Submit", class: "btn btn-success btn-block" %>
    <% end %>
  </div>

任何帮助将不胜感激,谢谢!

整个日志:

Started POST "/admin/add_question" for 10.240.1.4 at 2017-11-12 15:39:10 +0000
Cannot render console from 10.240.1.4! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by QuestionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"AFcHCrw6Dthasdesh5DNgdsxmFICxxPznyXtPDtxnp8zokpyZSeRHqiGC+K4SBMHehxYBgUXra30KTCj/AxUg==", "question"=>{"subject_id"=>"39", "category_id"=>"1", "hasdiagram"=>"1", "diagram"=>#<ActionDispatch::Http::UploadedFile:0x007f8064f72100 @tempfile=#<Tempfile:/tmp/RackMultipart20171112-8403-ewq7ng.png>, @original_filename="187c38aa17afdc65ba5c1c5239219686.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"question[diagram]\"; filename=\"187c38aa17afdc65ba5c1c5239219686.png\"\r\nContent-Type: image/png\r\n">, "question"=>"a", "option1"=>"a", "option2"=>"a", "option3"=>"a", "option4"=>"a"}, "commit"=>"Submit"}
Command :: file -b --mime '/tmp/c5769bbf6ba9051718d946344886703720171112-8403-1v5gz0n.png'
Completed 500 Internal Server Error in 13ms (ActiveRecord: 0.0ms)

NoMethodError (undefined method `before' for false:FalseClass):
  app/controllers/questions_controller.rb:11:in `create'


  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (6.0ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.4/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (27.9ms)

【问题讨论】:

  • @spickermann 我添加了整个错误和我提交的表单
  • @spickermann 是的,这是第 12 行,我删除了显示操作
  • @spickermann 好的,我添加了提交表单时的服务器日志
  • 所以这是我到目前为止得到的......错误解释:stackoverflow.com/a/44099729/8503822 我唯一能想到的“之前”就是您对内容类型的验证。有些东西返回 false 并将其运行到验证中,这会给你这个错误。
  • 您是否将所有验证都包含在问题中?或者您是否从Question 模型中移除了部件?

标签: ruby-on-rails ruby ruby-on-rails-4 paperclip


【解决方案1】:

该表单似乎缺少上传文件时需要的multipart: true。像这样更改form_tag

<%= form_for @question, 
             url: { controller: 'questions', action: 'create'}, 
             method: :post,
             multipart: true do |f| %>

在 Rails 指南中了解有关 uploading files 的更多信息。

【讨论】:

  • 感谢您的回答。我在表单中添加了 multipart: true ,但它并没有消除错误。还有什么我做错了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
相关资源
最近更新 更多