微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决添加带有回形针的图像失败 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 %>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。