带有Dropzonejs的ActionView :: MissingTemplate

时间:2018-09-01 14:46:39

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

当我在放置区中上载图像时,我遇到了这个错误(就像表单中的预览器一样),但看不到正在发生什么。 我有所有的想法……

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_guitar ,  我正在使用回形针来上传附件

中的图像

我相信我的模型还可以:

attachment.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>

1 个答案:

答案 0 :(得分:0)

该错误告诉您缺少视图。具体来说,您的show.html.erb文件夹中名为views/finished_guitars的文件。您的show.html.erb文件夹中是否有一个名为views/finished_guitars/的文件?

如果您查看错误,则可以看到我的意思ActionView::MissingTemplate (Missing template finished_guitars/show。这是一个ActionView错误,而错误恰好是MissingTemplate,这意味着缺少一个视图文件,然后它告诉您哪个finished_guitars/show

我也只想确定文件夹名称中是否有错字? app/views/finished_guitasr/_form.html.erb上写着.../finished_guitasr/...的地方。还是您在问题中这样写过?