Rails回形针错误未定义方法`before'表示false:FalseClass将图像附加到模型时

时间:2021-12-03 23:26:17

I have a model named Question that I want to attach a file to (diagram) using Paperclip (5.0.0). When the model is saved I get this error:

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

undefined method `before' for false:FalseClass

My Model:

我的型号:

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

My Controller and the line the error occurs on

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

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

Whole error:

整个错误:

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

My form:

我的表格:

<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>

Any help would be greatly appreciated, thanks!

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

Entire log:

整个日志:

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)

1 个解决方案

#1


1  

It looks like the form is missing a multipart: true that is needed when uploading files. Change the form_tag like this:

看起来表单缺少multipart:true在上传文件时需要。像这样更改form_tag:

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

Read more about uploading files in the Rails guides.

阅读有关在Rails指南中上载文件的更多信息。

#1


1  

It looks like the form is missing a multipart: true that is needed when uploading files. Change the form_tag like this:

看起来表单缺少multipart:true在上传文件时需要。像这样更改form_tag:

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

Read more about uploading files in the Rails guides.

阅读有关在Rails指南中上载文件的更多信息。