Do I have a problem with the paperclip in rails?

1

my problem is the following I want to make a small data bank in which I want to upload images of each bank logo. I use the gem of a paperclip and it really has given me headaches, it helps me in the connection with the user of devise but my problem eradicates in this error:

No handler found for "image.jpg"

  # POST /databanks.json
  def create
    @databank = Databank.new(databank_params)

    respond_to do |format|
      if @databank.save

I really do not understand why he does not keep the folder for me and when I validate it in the params, I get that.

schema.rb:

ActiveRecord::Schema.define(version: 20180219174845) do

  create_table "activities", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "contract_types", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "contracts", force: :cascade do |t|
    t.string "currency"
    t.string "name"
    t.decimal "amount"
    t.integer "contract_type_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["contract_type_id"], name: "index_contracts_on_contract_type_id"
  end

  create_table "databanks", force: :cascade do |t|
    t.string "title"
    t.string "username"
    t.string "password"
    t.string "repeat_password"
    t.string "url"
    t.string "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "thumbnail_file_name"
    t.string "thumbnail_content_type"
    t.integer "thumbnail_file_size"
    t.datetime "thumbnail_updated_at"
  end

  create_table "groups", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "memberships", force: :cascade do |t|
    t.integer "user_id"
    t.integer "group_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["group_id"], name: "index_memberships_on_group_id"
    t.index ["user_id"], name: "index_memberships_on_user_id"
  end

  create_table "permissions", force: :cascade do |t|
    t.integer "group_id"
    t.integer "activity_id"
    t.boolean "c"
    t.boolean "r"
    t.boolean "u"
    t.boolean "d"
    t.boolean "p"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["activity_id"], name: "index_permissions_on_activity_id"
    t.index ["group_id"], name: "index_permissions_on_group_id"
  end

  create_table "project_statuses", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.decimal "percentage"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "project_types", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "recruitments", force: :cascade do |t|
    t.string "Position"
    t.decimal "salary"
    t.date "begin"
    t.date "finish"
    t.integer "contract_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["contract_id"], name: "index_recruitments_on_contract_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.string "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string "unconfirmed_email"
    t.integer "failed_attempts", default: 0, null: false
    t.string "unlock_token"
    t.datetime "locked_at"
    t.string "name", default: "", null: false
    t.string "bank"
    t.string "type_of_bank_account"
    t.integer "bank_account_number", limit: 20
    t.string "email_paypal"
    t.integer "identification_card", limit: 8
    t.integer "phone_number", limit: 20
    t.integer "house_number", limit: 20
    t.string "skype"
    t.text "abilities"
    t.text "languages"
    t.string "invitation_token"
    t.datetime "invitation_created_at"
    t.datetime "invitation_sent_at"
    t.datetime "invitation_accepted_at"
    t.integer "invitation_limit"
    t.integer "invited_by_id"
    t.string "invited_by_type"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "avatar_file_name"
    t.string "avatar_content_type"
    t.integer "avatar_file_size"
    t.datetime "avatar_updated_at"
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
  end

end

databanks_controller.rb:

class DatabanksController < ApplicationController
  before_action :set_databank, only: [:show, :edit, :update, :destroy]

  # GET /databanks
  # GET /databanks.json
  def index
    @databanks = Databank.all
  end

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

  # GET /databanks/new
  def new
    @databank = Databank.new
  end

  # GET /databanks/1/edit
  def edit
  end

  # POST /databanks
  # POST /databanks.json
  def create
    @databank = Databank.new(databank_params)

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

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

  # DELETE /databanks/1
  # DELETE /databanks/1.json
  def destroy
    @databank.destroy
    respond_to do |format|
      format.html { redirect_to databanks_url, notice: 'Databank was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def databank_params
      params.require(:databank).permit(:title, :thumbnail,:username, :password, :repeat_password, :url, :description )
    end
end

databank.rb:

class Databank < ApplicationRecord
  has_attached_file :thumbnail, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "default_image"
  validates_attachment_content_type :thumbnail, content_type: /\Aimage\/.*\z/
end

migrations:

class CreateDatabanks < ActiveRecord::Migration[5.1]
  def change
    create_table :databanks do |t|
      t.string :title
      t.string :username
      t.string :password
      t.string :repeat_password
      t.string :url
      t.string :description

      t.timestamps
    end
  end
end

add_attachment_thumbnail_to_databanks.rb:

class AddAttachmentThumbnailToDatabanks < ActiveRecord::Migration[4.2]
  def self.up
    change_table :databanks do |t|
      t.attachment :thumbnail
    end
  end

  def self.down
    remove_attachment :databanks, :thumbnail
  end
end

_form.html.erb

    <%= form_with(model: databank, local: true) do |form| %>
<% if databank.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(databank.errors.count, "error") %> prohibited this databank from being saved:</h2>
  <ul>
    <% databank.errors.full_messages.each do |message| %>
    <li><%= message %></li>
    <% end %>
  </ul>
</div>
<% end %>
<div class="field">
  <%= form.label :title %>
  <%= form.text_field :title, id: :databank_title %>
</div>
<div class="field">
  <%= form.label :username %>
  <%= form.text_field :username, id: :databank_username %>
</div>
<div class="field">
  <%= form.label :password %>
  <%= form.text_field :password, id: :databank_password %>
</div>
<div class="field">
  <%= form.label :repeat_password %>
  <%= form.text_field :repeat_password, id: :databank_repeat_password %>
</div>
<div class="field">
  <%= form.label :url %>
  <%= form.text_field :url, id: :databank_url %>
</div>
<div class="field">
  <%= form.label :description %>
  <%= form.text_field :description, id: :databank_description %>
</div>
  <%= form_for @databank, html: { multipart: true } do |f| %>
    <%= f.file_field :thumbnail %>
  <div class="actions">
    <%= form.submit %>
  </div>
  <% end %>
<% end %>
    
asked by Tysaic 19.02.2018 в 19:31
source

1 answer

0

Your problem is that you are mixing 2 forms, form_with(model: databank, local: true) and form_for @databank, html: { multipart: true } . when you should only use the first of them with the options of the second.
It is not part of the problem, but you are also using model: databank without the symbol @ , so you do not access the instance of your model, that is why you had to add a id: :databank_algo to each of your text fields, when that should not be necessary.
At the end the form would be as follows:

<%= form_with(model: @databank, local: true, html: { multipart: true }) do |form| %>
  <% if @databank.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@databank.errors.count, "error") %> prohibited this databank from being saved:</h2>
      <ul>
        <% @databank.errors.full_messages.each do |message| %>
          <li>
            <%= message %>
          </li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>
  <div class="field">
    <%= form.label :username %>
    <%= form.text_field :username %>
  </div>
  <div class="field">
    <%= form.label :password %>
    <%= form.text_field :password %>
  </div>
  <div class="field">
    <%= form.label :repeat_password %>
    <%= form.text_field :repeat_password %>
  </div>
  <div class="field">
    <%= form.label :url %>
    <%= form.text_field :url %>
  </div>
  <div class="field">
    <%= form.label :description %>
    <%= form.text_field :description %>
  </div>
  <div class="field">
    <%= form.label :thumbnail %>
    <%= form.file_field :thumbnail %>
  </div>
  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

Be careful, that local: true should only be added if you are using ujs and you want this request to be synchronous. But, it is not necessary.

    
answered by 20.02.2018 / 19:49
source