Home Rails paperclip error undefined method `before' for false:FalseClass when attaching image to model
Reply: 1

Rails paperclip error undefined method `before' for false:FalseClass when attaching image to model

RT5754 Published in 2017-11-12 15:09:11Z

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:

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


My Controller and the line the error occurs on

class QuestionsController < ApplicationController

  def new
    @question = Question.new
    render :new

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


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


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

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

Any help would be greatly appreciated, thanks!

Entire log:

Started POST "/admin/add_question" for at 2017-11-12 15:39:10 +0000
Cannot render console from! Allowed networks:, ::1,
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 Reply to 2017-11-12 15:22:26Z

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

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

Read more about uploading files in the Rails guides.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31835 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO