带 RJS 的 Sortable_element 不起作用

发布于 2024-08-28 12:17:28 字数 2402 浏览 5 评论 0原文

我有一个图像列表,用户可以在其中安排他们的订单。

当用户上传图像时,我希望列表仍然可以排序。

我正在使用此处描述的类似上传: http://kpumuk.info/ruby-on-rails/in-place-file-upload-with-ruby-on-rails/

请帮忙。

以下是在视图文件中上传的代码:

  <% form_for [:admin, @new_image], :html => { :target => 'upload_frame', :multipart => true } do |f| %>
    <%= hidden_field_tag :update, 'product_images'%>
    <%= f.hidden_field :image_owner_id %>
    <%= f.hidden_field :image_owner_type %>
    <%= f.file_field :image_file %><br />
    or get image from this URL: <%= f.text_field :image_file_url %>
    <%= f.hidden_field :image_file_temp %><br />
    <%= f.submit "Upload Image" %>
  <% end %>

在控制器视图中:

  def create
    @image = Image.new(params[:image])
    logger.debug "params are #{params.inspect}"
    if @image.save
      logger.debug "initiating javascript now"
      responds_to_parent do
        render :update do |page|
          logger.debug "javascript test #{sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)}"
          page << "show_notification('Image Uploaded');"
          page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
          page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)
        end
      end
      #render :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
    else
      responds_to_parent do
        render :update do |page|
          page << "show_notification('Image Upload Error');"
        end
      end
    end
  end

或者,重新表述问题:

运行此:

  page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
  page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)

不会添加可排序列表功能。

请帮忙,

谢谢

I have a list of images where user can arrange their orders.

When user uploaded an image, I want the list to still be sortable.

I am using a similar upload that was described here: http://kpumuk.info/ruby-on-rails/in-place-file-upload-with-ruby-on-rails/

Please help.

Here are the code for upload in view file:

  <% form_for [:admin, @new_image], :html => { :target => 'upload_frame', :multipart => true } do |f| %>
    <%= hidden_field_tag :update, 'product_images'%>
    <%= f.hidden_field :image_owner_id %>
    <%= f.hidden_field :image_owner_type %>
    <%= f.file_field :image_file %><br />
    or get image from this URL: <%= f.text_field :image_file_url %>
    <%= f.hidden_field :image_file_temp %><br />
    <%= f.submit "Upload Image" %>
  <% end %>

And in controller view:

  def create
    @image = Image.new(params[:image])
    logger.debug "params are #{params.inspect}"
    if @image.save
      logger.debug "initiating javascript now"
      responds_to_parent do
        render :update do |page|
          logger.debug "javascript test #{sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)}"
          page << "show_notification('Image Uploaded');"
          page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
          page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)
        end
      end
      #render :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
    else
      responds_to_parent do
        render :update do |page|
          page << "show_notification('Image Upload Error');"
        end
      end
    end
  end

Or, to rephrase the question:

Running this:

  page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
  page << sortable_element("product_images", :url => sort_admin_images_path, :handle => "handle", :constraint => false)

Will NOT adding sortable list feature.

Please help,

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2024-09-04 12:17:28

我认为问题很可能是可排序是在页面首次加载时创建的。我已经遇到过这个问题好几次了。向列表添加新元素意味着您必须再次调用可排序函数才能将新元素包含在排序中。

我不熟悉原型/scriptaculous,但可能有一种方法可以让可排序元素侦听将元素添加到列表的事件。 jQuery 有一组重新绑定事件,它们将响应添加到 DOM 的新元素,这可能与其他库中的情况类似。

I think the problem may well be that the sortable is created when the page first loads. I have run into this issue a couple of times. Adding a new element to the list means you have to call the sortable function again to have the new element included in the sorting.

I am not across prototype/scriptaculous, but there may be a way to have the sortable element listen for events that add elements to the list. jQuery has a set of rebinding events that will respond to new elements added to the DOM, might be something simialr in the other libs.

一抹微笑 2024-09-04 12:17:28

找到了!

代替:

  page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}

使用

  page.replace params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}

Found it!

Instead of:

  page.replace_html params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}

Use

  page.replace params[:update], :partial => '/admin/shared/editor/images', :locals => {:object => @image.image_owner, :updated_image => @image}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文