【问题标题】:ActionView::MissingTemplate with Dropzonejs带有 Dropzonejs 的 ActionView::MissingTemplate
【发布时间】:2018-09-01 14:46:39
【问题描述】:

当我在 dropzone 中上传图像时(就像表单中的预览器),我遇到了这个错误,但我看不到发生了什么。 我有所有的意见......

ActionView::MissingTemplate (Missing template finished_guitars/show, application/show with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
  * "/Users/Billy/code/guitar_app/app/views"
):

我有两个模型:

finished_guitarattachment 有关系, 我正在使用回形针附件

中上传图片

我相信我的模型没问题:

附件.rb

class Attachment < ApplicationRecord
    belongs_to :finished_guitar
    has_attached_file :image, styles: { medium: "300x300>", thumb: "120x120>" }, default_url: "/images/:style/missing.png"
    validates_attachment_content_type :image, :content_type => ["imagse/jpg", "images/jpeg", "images/png"]
end

finished_guitar.rb

class FinishedGuitar < ApplicationRecord
    accepts_nested_attributes_for :attachments, allow_destroy: true
    has_many :attachments
end

我一定是在控制器的某个地方错了:

attachments_controller.rb

class AttachmentsController < ApplicationController

  def create
    @attachment = Attachment.new(attachment_params)
    respond_to do |format|
      if @attachment.save
        format.html { redirect_to @finished_guitar, notice: 'Attachment was successfully created.' }
        format.json { render :show, status: :created, location: @attachment }
      else
        format.html { render :new }
        format.json { render json: @attachment.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def attachment_params
      params.require(:attachment).permit(:id, :finished_guitar_id, :image)
    end
end

finished_guitars_controller.rb

class FinishedGuitarsController < ApplicationController

    def index
        @finished_guitars = FinishedGuitar.all
    end

    def show
        @finished_guitar = FinishedGuitar.find(params[:id])
        @attachments = @finished_guitar.attachments.all
    end

    def new
        @finished_guitar = FinishedGuitar.new
        @attachments = @finished_guitar.attachments.build
    end


    def create  
        @finished_guitar = FinishedGuitar.new(finished_guitar_params)
    respond_to do |format|
      if @finished_guitar.save
        unless params[:attachments].nil?
            params[:attachments]['image'].each do |a|
                @finished_guitar = @finished_guitar.attachments.create!(:image => a)
            end
        end
        format.html { redirect_to @finished_guitar, notice: 'Guitartest was successfully created.' }
        format.json { render :show, status: :created, location: @finished_guitar }
      else
        format.html { render :new }
        format.json { render json: @finished_guitar.errors, status: :unprocessable_entity }
      end
    end
    end

    def destroy
        @finished_guitar = FinishedGuitar.find(params[:id])
        @finished_guitar.destroy
    end


    private
        def finished_guitar_params
            params.require(:finished_guitar).permit(:title, :description, attachments_attributes: [:finished_guitar_id, :id, :image])
        end
end

编辑:

app/views/finished_guitars/_form.html.erb

<div class="content">
    <%= simple_form_for @finished_guitar, html: {multipart: true, id: "my-dropzone", class: "dropzone"} do |f| %>
        <%= f.input :title %>
        <%= f.input :description %>

        <div class="dz-message needsclick">
            <h3>Drop file here</h3> or
            <strong>click</strong> to upload
        </div>
        <div class="fallback">

            <%= simple_fields_for :attachments do |ff| %>
                <%= ff.input_field :image %>
            <% end %>
        </div>
            <%= f.button :submit, class: "btn btn-primary" %>
    <% end %>

</div>

【问题讨论】:

    标签: ruby-on-rails paperclip dropzone.js


    【解决方案1】:

    该错误告诉您缺少视图。特别是您的views/finished_guitars 文件夹中名为show.html.erb 的文件。您的views/finished_guitars/ 文件夹中有一个名为show.html.erb 的文件吗?

    如果您查看错误,您会明白我的意思 ActionView::MissingTemplate (Missing template finished_guitars/show 。这是一个ActionView 错误,而错误正是MissingTemplate,这意味着缺少一个视图文件,然后它会告诉你哪个finished_guitars/show

    我还想确认一下,文件夹名称中是否有拼写错误? app/views/finished_guitasr/_form.html.erb 上面写着.../finished_guitasr/...。还是你只是在问题中这样写?

    【讨论】:

    • 这是问题中的错字对不起,我会更新。我已经创建了所有视图,所以没有一个丢失。当我上传图像时,此错误消息出现在我的 rails 控制台中。即使附件没有上传,我仍然可以发布我的finished_guitarshow.html.erb 显示标题和描述,但不显示图像。
    猜你喜欢
    • 2010-11-28
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多