【发布时间】: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_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>
【问题讨论】:
标签: ruby-on-rails paperclip dropzone.js