添加带有回形针的图像失败 NoHandlerError

问题描述

在 rails 6 中使用 Paperclip 6.1.0 时出现此错误

Paperclip::AdapterRegistry::NoHandlerError (没有找到“logo.jpg”的处理程序):

在我的模型中,我有

 class Movie < ApplicationRecord

  has_many :screens
  has_many :tickets


  has_attached_file :blob,styles: {
      thumb: '100x100>',square: '200x200#',medium: '300x300>'
  }

  # Validate the attached image is image/jpg,image/png,etc
  validates_attachment_content_type :blob,:content_type => /\aimage\/.*\Z/

end

异常发生在:

  def create
    @movie = Movie.new(movie_params)
    if @movie.save
      redirect_to @movie,notice: 'Movie was successfully created.'
    else

带参数:

    Parameters:

{"authenticity_token"=>"fzm4JrC/eGQsDUS04w52lfI7B5hC6agpSRtyn+4BSayWxRt1RG/lZDNypxxWVmcSZfaW6+HQ+auNJm7p43p/LQ==","movie"=>{"title"=>"test","description"=>"345","movie_length"=>"4r","age_limit"=>"345","price"=>"345","category"=>"345","blob"=>"Screen Shot 2020-12-30 at 10.24.49.png"},"commit"=>"Create Movie"}

_form.html.erb:

<%= form_for @movie do |f|%>
  <%= form_for @movie,html: { multipart: true } do |f|%>
    <div class="form-group">
      <% if @movie.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@movie.errors.count,"error") %> prohibited this movie from being saved:</h2>
          <ul>
            <% @movie.errors.full_messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
          </ul>
        </div>
      <% end %>
<%# <div class="field"> %>
      <%= f.label :title %><br>
      <%= f.text_field :title,class: "form-control",placeholder: "Title" %>
    </div>
   
      <div class="field">
  <%= f.label :blob %><br>
  <%= f.text_field :blob,placeholder: "blob" %>
</div>

    <div class="actions">
      <%= f.submit %>
<%# f.submit will automatically give it a create or update button %>
    </div>
  <% end %>
<% end %>

有人知道这是怎么回事吗?

解决方法

您有两个对 form_for 嵌套的调用。这将创建 <form ...><form...>,它不是有效的 HTML。 Form elements may not be nested inside of each other 并且行为是高度不可预测的。通常提交按钮会提交外部表单元素,但由于这是非标准的,任何事情都可能发生。

移除对 <%= form_for(@movie) do |f| %> 的外部调用并使用文件字段输入而不是文本输入:

<%= form_for @movie,html: { multipart: true } do |f|%>
  <div class="form-group">
    <% if @movie.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@movie.errors.count,"error") %> prohibited this movie from being saved:</h2>
          <ul>
            <% @movie.errors.full_messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
          </ul>
        </div>
    <% end %>
    <div class="field">
      <%= f.label :title %><br>
      <%= f.text_field :title,class: "form-control",placeholder: "Title" %>
    </div>
   
    <div class="field">
      <%= f.label :blob %><br>
      <%= f.file_field :blob,placeholder: "blob" %>
    </div>

    <div class="actions">
      <%= f.submit %>
    </div>
  </div>
<% end %>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...