3

我正在尝试在 rails_admin 中建立拖放图像关联。

我有地方模型和许多与那个地方相关的图像。图片将在之前上传并与地点相关联。

我在 rails_admin 中创建了自定义操作。

rails_admin.rb

# Registering Custom Actions
  module RailsAdmin
    module Config
      module Actions
       class ImageAssociate < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)
       end
      end
    end
  end

# Adding Custome action to RA actions
  config.actions do
    dashboard
    index
    new

    show
    edit
    delete
    image_associate
  end

lib 目录中的rails_admin_image_associate.rb

require 'rails_admin/config/actions'
require 'rails_admin/config/actions/base'

module RailsAdminImageAssociate
end

module RailsAdmin
  module Config
    module Actions
      class ImageAssociate < RailsAdmin::Config::Actions::Base

        register_instance_option :link_icon do
          'icon-picture'
        end

        # Its Memeber Function
        register_instance_option :member do
          true
        end


        register_instance_option :controller do
        Proc.new do
          @associated_images = @object.images
          @images = Image.all - @associated_images
          # binding.pry
          @image_type = @images.collect(&:asset_type)

          # flash[:notice] = "You have approved the review titled: #{@object.name}"
        end
       end
      end
    end
  end
end

我将它添加到 rails_admin.rb

require Rails.root.join('lib', 'rails_admin_image_associate.rb')

意见/rails_admin/main/image_associate.rb

<%= stylesheet_link_tag "rails_admin/image_associate.css" %>

<div id="all_images" class="span4">
    <select name="image_type" id="image_type">
        <option value="all">all</option>
        <% @image_type.each do |type| %>
            <option value="<%=type%>"><%=type%></option>
        <%end%>
    </select>
    <% @images.each do |image| %>
        <img src="<%=image.file.thumb%>">
    <%end%>
</div>
<div id="associated_images" class="span7">
    <% @associated_images.each do |aimage| %>
        <img src="<%=aimage.file.thumb%>">
    <%end%>
</div>

<div class="clearfix">

</div>

现在我需要在选择框“image_type”上触发一个“更改事件”。我覆盖了 rails_admin 中的“ui.js”并包含了自定义 js 文件

资产/javascript/rails_admin/custom/ui.js

//= require_tree .

资产/javascript/rails_admin/custom/image_associate.js

$('#image_type').on('change',function () {
alert("change triggered");
});

我可以看到这个文件是在前端加载的。但是事件没有触发。

截屏

在此处输入图像描述

4

1 回答 1

4

尝试:

$(document).on('change', '#image_type',function () {
  alert("change triggered");
});
于 2013-08-18T06:04:12.973 回答