使用回形针添加图像失败 NoHandlerError

adding image with paperclip failed 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, class: "form-control", 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, class: "form-control", placeholder: "blob" %>
    </div>

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