如何使用带回形针的remotipart上传图像并实时显示

时间:2016-06-20 16:00:58

标签: javascript ruby-on-rails ruby-on-rails-5 remotipart

这是我的代码

remoti.coffe

$(document).on "change", "#post_image", (en)->
$(this).submit()
来自帖子的

_form.haml

    = form_for @post, remote:true do |f|
  - if @post.errors.any?
    #error_explanation
      %h2= "#{pluralize(@post.errors.count, "error")} prohibited this post from being saved:"
      %ul
        - @post.errors.full_messages.each do |msg|
          %li= msg
  .input-field
    = f.file_field :image
  .input-field
    = f.text_area :body, class: "materialize-textarea", rows: "1"
    = f.label :body, "Comparte con la comunidad"

  .actions
    = f.submit 'Publicar', class: "btn waves-effect waves-light deep-orange accent-2 right"

以JS回复

show.js.coffee

$("#posts").prepend("<%= j render 'post', post: @post%>")

渲染_post.haml

  .card
  .card-content
    .text-left#image_view
      = post.user.email
      =image_tag post.image(:medium)

请帮帮我 对不起我的英文

0 个答案:

没有答案