【问题标题】:adding image with paperclip failed NoHandlerError用回形针添加图像失败 NoHandlerError
【发布时间】:2021-01-04 18:51:50
【问题描述】:

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

有人知道怎么回事吗?

【问题讨论】:

  • blob 应该是电影的预览图像吗?这是一个非常奇怪的名字选择。
  • 是的,我可以改变它。但我想这不是问题?
  • 不,问题很可能是您发送常规表单数据而不是多部分。参数应包含ActionDispatch::Http::UploadedFile 的实例,而不仅仅是字符串。我猜这是通过表单提交(而不是测试)发送的?可以添加表格吗?
  • @max 我已经添加了表单。

标签: ruby-on-rails ruby rubygems paperclip


【解决方案1】:

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

删除对&lt;%= form_for(@movie) do |f| %&gt; 的外部调用并使用文件字段输入而不是文本输入:

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

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多