1

Mi problema es el siguiente, deseo hacer un pequeño banco de datos en el cual quiero subir imágenes de logo de cada banco. Uso la gema de paperclip y de verdad me ha dado dolores de cabeza, me sirve en la conexión con el usuario de devise pero mi problema radica en este error:

No handler found for "image.jpg"

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

    respond_to do |format|
      if @databank.save

No entiendo porqué no me guarda la carpeta y cuando la valido en los params me sale eso.

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 %>
2
  • ¿Cual es el código de tu formulario? Agrégalo a tu pregunta, el problema podría estar ahí. Commented el 20 feb. 2018 a las 12:48
  • Aquí esta el codigo ya modificado
    – Tysaic
    Commented el 20 feb. 2018 a las 13:59

1 respuesta 1

0

Tu problema es que estás mezclando 2 forms, form_with(model: databank, local: true) y form_for @databank, html: { multipart: true }. cuando solo deberías usar el primero de ellos con las opciones del segundo.
No es parte del problema, pero además estás usando model: databank sin el símbolo @, por lo cual no accedes a la instancia de tu modelo, es por eso que has tenido que agregar un id: :databank_algo a cada uno de tus text fields, cuando eso no debería ser necesario.
Al final el formulario quedaría como sigue:

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

Ojo, que local: true solo deberías agregarlo si estás usando ujs y quieres que este request sea síncrono. Sino, no es necesario.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.