尝试上传图像时,不会触发js.erb文件

时间:2012-10-15 17:05:59

标签: ruby-on-rails ruby ruby-on-rails-3 fancybox simple-form

我在这个表格中有一个fancybox框:

<%= simple_form_for(@message, :remote => true, :html => {:multipart => true}, :url => { :controller => "users/messages", :action => "create"} ) do |f| %>
 <%= f.error_notification %>
 <%= f.input :subject, :input_html => { :maxlength => 500} %>
 <%= f.input :message_image, :as => :file %>
 <%= f.button :submit, t('.send_message'), :class => 'btn-large btn-primary', "data-disable-with" => t('.send_message'), :confirm => t('.sure_send_message') %>
<% end %>

这是我的创作动作:

def create
 @message = Message.new(params[:message])
 respond_to do |format|
  if @message.save
   format.js { render :partial => "users/messages/templates/sent_by_user/create" }
  else
   format.js { render :partial => "users/messages/templates/sent_by_user/nosave" }
  end
 end 
end

如果我不尝试上传图片,文件create.js.erbnosave.js.erb文件正常工作

如果我尝试上传图片,则不会呈现此文件。

但是在我的日志中我可以看到:

Processing by Users::MessagesController#create as JS
  Parameters: {"message"=>{"message_image"=>#<ActionDispatch::Http::UploadedFile:0xf7a85c8 @original_filename="cara_enfadada.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"message[message_image]\"; filename=\"cara_enfadada.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20121015-21710-6oxtn2>>, "subject"=>"estrella de neutronessssssssssss"}, "commit"=>"Enviar mensaje", "utf8"=>"✓", "authenticity_token"=>"mfSsl0HafWoW86YiO2Hj7zMepAITZlHHBtUqW0wzubU=", "remotipart_submitted"=>"true", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01", "locale"=>"es"}

Rendered users/messages/templates/sent_by_user/_nosave.js.erb (0.2ms)
Completed 200 OK in 498ms (Views: 1.5ms | Solr: 0.0ms)

在Google Chrome控制台上,我收到了下一个提醒:

Resource interpreted as Document but transferred with MIME type text/javascript:

我正在使用remotipart gem,但我没有发现错误...... 错误在哪里? 谢谢

1 个答案:

答案 0 :(得分:0)

我永远不会执行js.erb,因为可以使用ajax直接上传文件(在你的情况下为图像)

您可能需要考虑 remotipart pupload jqueryfile_upload uplodify 来实现

我确定remotipart使用iframe

Pupload使用运行时支持html5或flash

uploadify使用flash

不确定jqueryfile_upload从未尝试过它

祝你好运