Rails回形针错误未定义方法`之前' for false:将图像附加到模型时为FalseClass

时间:2017-11-12 15:09:11

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

我有一个名为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)

1 个答案:

答案 0 :(得分:1)

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

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

在Rails指南中阅读有关uploading files的更多信息。