我正在尝试使用回形针和tinymce-rails-image-upload演示后的this上传图片。我正在上传问题模型中的图像(属于评论)。当我上传时,我收到“来自服务器的错误响应”消息,并且图像未显示在编辑器中,但它确实会创建一个新问题。
以上是我上传后的日志:
Processing by TinymceAssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"auth token", "hint"=>"", "file"=>#<ActionDispatch::Http::UploadedFile:0x000001025a2780 @tempfile=# <File:/var/folders/t4/86vsrmds42j84r36kwpng7k00000gn/T/RackMultipart20150207- 12522-9rj6xq>, @original_filename="applecash.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"applecash.jpg\"\r\nContent-Type: image/jpeg\r\n">, "alt"=>""}
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/t4/86vsrmds42j84r36kwpng7k00000gn/T/RackMultipart20150207-12522-9rj6xq[0]' 2>/dev/null
Unpermitted parameters: utf8, authenticity_token
(0.1ms) begin transaction
Question Load (0.5ms) SELECT "questions".* FROM "questions" WHERE (questions.position IS NOT NULL) AND (1 = 1) ORDER BY questions.position DESC LIMIT 1
Binary data inserted for `string` type on column `file_content_type`
SQL (0.8ms) INSERT INTO "questions" ("created_at", "file_content_type", "file_file_name", "file_file_size", "file_updated_at", "position", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Sun, 08 Feb 2015 18:35:07 UTC +00:00], ["file_content_type", "image/jpeg"], ["file_file_name", "timcook.jpg"], ["file_file_size", 120040], ["file_updated_at", Sun, 08 Feb 2015 18:35:07 UTC +00:00], ["position", 9], ["updated_at", Sun, 08 Feb 2015 18:35:07 UTC +00:00]]
(7.3ms) commit transaction
Completed 200 OK in 68ms (Views: 0.6ms | ActiveRecord: 8.7ms)
这是tinymce控制器:
class TinymceAssetsController < ApplicationController
respond_to :json
def create
geometry = Paperclip::Geometry.from_file params[:file]
question = Question.create params.permit(:file, :alt, :hint)
render json: {
question: {
url: question.file.url,
height: geometry.height.to_i,
width: geometry.width.to_i
}
}, layout: false, content_type: "text/html"
end
end
问题控制器创建动作看起来像这样(我是否将@comment和附加参数添加到tinymce控制器?):
def create
@comment = Comment.find(params[:comment_id])
@question = @comment.questions.new(question_params)
end
...
def question_params
params.require(:question).permit(:body, :comment_id, :user_id, :file, :position)
end
问题模型:
class Question < ActiveRecord::Base
has_attached_file :file
end
和编辑观点:
<%= simple_form_for [@comment, Question.new] do |f| %>
<%= f.text_area :body, :class => "tinymce", :rows => 10, :cols => 60 %>
<% end %>
<%= tinymce plugins: ["uploadimage"] %>
我很困惑为什么它为每个上传的图片创建一个新问题以及仍然导致“错误响应”错误的原因。
答案 0 :(得分:3)
似乎tinymce-rails-imageupload期待&#34;图像&#34;响应上传的文件
render json: {
image: {
url: question.file.url,
height: geometry.height.to_i,
width: geometry.width.to_i
}
}, layout: false, content_type: "text/html"