Rails active storage UnknownAttributeError

I installed Active storage to download / display images. In the form, when I select the image and send my form, I have the following answer:

ActiveModel :: UnknownAttributeError (unknown attribute ‘image’ for Book):

Ruby on Rails tries to add the image in the book table (so obviously the image field in the book table does not exist) and not in the active storage tables.

Another point also, when installing activestorage, my configuration file is called storage_services.yml and not storage.yml as I saw on the net. Do you think the problem can come from there.

Do I need to add an association or other for my image to be stored in active storage?
RoR tries to add the image in the contact table and not in the active storage tables. I feel that active storage is not active.

Thanks a lot for your help

However, I added the gem ‘activestorage’ 0.1 to my Gemfile file correctly, and all the installation is going well.

I redo a test with a model Contact but always the same error

To Install ActiveStorage: In the Gemfile , I added the gem ‘activestorage’, ‘~> 0.1’

gem 'rails', '~> 5.1.6'
gem 'mysql2', '>= 0.3.18', '< 0.6.0'
gem 'puma', '~> 3.7'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'activestorage', '~> 0.1'

File environnements/development.rb :

Rails.application.configure do
config.cache_classes = false
config.active_storage.service = :local
config.eager_load = false
...

File config/storage_services.yml :

test:
  service: Disk
    root: <%= Rails.root.join("tmp/storage") %>
local:
  service: Disk
    root: <%= Rails.root.join("storage") %>

File application.js :

//= require rails-ujs
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .

When I add //= require activestorage in the file application.js, I have this error:

Started GET "/contacts?locale=fr" for 127.0.0.1 at 2018-10-08 20:05:04 +0200 (19.0ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 (0.5ms) SELECT `schema_migrations` . `version` FROM `schema_migrations` ORDER BY `schema_migrations` . `version` ASC Processing by ContactsController#index as HTML Parameters: {"locale"=>"fr"} Rendering contacts/index.html.erb within layouts/application Contact Load (2.5ms) SELECT `contacts` .* FROM `contacts` Rendered contacts/index.html.erb within layouts/application (43.2ms) Completed 500 Internal Server Error in 3698ms (ActiveRecord: 6.4ms)

ActionView::Template::Error (couldn't find file 'activestorage' with type 'application/javascript' Checked in these paths: C:/Sites/comptabox/app/assets/config C:/Sites/comptabox/app/assets/images C:/Sites/comptabox/app/assets/javascripts C:/Sites/comptabox/app/assets/stylesheets C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/jquery-rails-4.3.3/vendor/assets/javascripts C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/coffee-rails-4.2.2/lib/assets/javascripts C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/actioncable-5.1.6/lib/assets/compiled C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/actionview-5.1.6/lib/assets/compiled C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/turbolinks-source-5.2.0/lib/assets/javascripts C:/Sites/comptabox/node_modules C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/popper_js-1.14.3/assets/javascripts C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bootstrap-4.1.3/assets/stylesheets C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bootstrap-4.1.3/assets/javascripts): 4: BookOdb 5: <%= csrf_meta_tags %> 6: 7: <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> 8: <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 9: <%= favicon_link_tag 'icone.jpg' %> 10:

app/assets/javascripts/application.js:15 app/views/layouts/application.html.erb:7:in `_app_views_layouts_application_html_erb__495421535_46057632'

In my model contact.rb , I had this:

class Contact < ApplicationRecord
   has_one_attached :image
end

In my view _form.html.erb, I added this field

<%= form.label :image %>
<%= form.file_field :image %>

When I don’t upload an image, all is OK:

Started GET "/contacts/2?locale=fr" for 127.0.0.1 at 2018-10-08 19:49:17 +0200 Processing by ContactsController#show as HTML Parameters: {"locale"=&gt;"fr", "id"=&gt;"2"} Contact Load (1.6ms) SELECT `contacts` .* FROM `contacts` WHERE `contacts` . `id` = 2 LIMIT 1 Rendering contacts/show.html.erb within layouts/application Rendered contacts/show.html.erb within layouts/application (1.0ms) User Load (0.5ms) SELECT `users` .* FROM `users` WHERE `users` . `id` = 1 ORDER BY `users` . `id` ASC LIMIT 1 Rendered layouts/_navbar.html.erb (10.4ms) Completed 200 OK in 491ms (Views: 442.6ms | ActiveRecord: 2.1ms)

When I validate the form with an picture, I have this error:

Started POST "/contacts?locale=fr" for 127.0.0.1 at 2018-10-08 19:50:59 +0200 Processing by ContactsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"UEakb5jb1urU32Cg5buC7PPJD3K61T6t2PxhAfCt501jezKkPPnK0Q2/XFT6wtd/oEVz1QsayasElI8NLxFm+g==", "contact"=&gt;{"title"=&gt;"z", "content"=&gt;"z", "image"=&gt;#, @original_filename="ticket_1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"contact[image]\"; filename=\"ticket_1.jpg\"\r\nContent-Type: image/jpeg\r\n"&gt;}, "commit"=&gt;"Créer un(e) Contact", "locale"=&gt;"fr"} Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

ActiveModel::UnknownAttributeError (unknown attribute 'image' for Contact.):
app/controllers/contacts_controller.rb:27:in `create'

This is my controller file

class ContactsController < ApplicationController
  before_action :set_contact, only: [:show, :edit, :update, :destroy]

  # GET /contacts
  # GET /contacts.json
  def index
    @contacts = Contact.all
  end

  # GET /contacts/1
  # GET /contacts/1.json
  def show
  end

  # GET /contacts/new
  def new
    @contact = Contact.new
  end

  # GET /contacts/1/edit
  def edit
  end

  # POST /contacts
  # POST /contacts.json
  def create
    @contact = Contact.new(contact_params)

    respond_to do |format|
      if @contact.save
        format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
        format.json { render :show, status: :created, location: @contact }
      else
        format.html { render :new }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /contacts/1
  # PATCH/PUT /contacts/1.json
  def update
    respond_to do |format|
      if @contact.update(contact_params)
        format.html { redirect_to @contact, notice: 'Contact was successfully updated.' }
        format.json { render :show, status: :ok, location: @contact }
      else
        format.html { render :edit }
        format.json { render json: @contact.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.json
  def destroy
    @contact.destroy
    respond_to do |format|
      format.html { redirect_to contacts_url, notice: 'Contact was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_contact
      @contact = Contact.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def contact_params
      params.require(:contact).permit(:title, :content, :image )
    end
end

Thanks a lot

I don’t think you’re supposed to install that version of the gem.

Rails gems like ActiveRecord & ActiveSupport always have the same version as the Rails version.

So for Rails 5.2, you would have:

  • activerecord 5.2
  • activesupport 5.2
  • actionpack 5.2

etc.

I think you need Rails 5.2 minimum for ActiveStorage, according to this:

Hope that helps :slight_smile:

When I validate the form with an picture, I have this error:

ActiveModel::MissingAttributeError (can’t write unknown attribute record_id):

Thanks or your help