【问题标题】:How to pass javascript variable from Rails view to action如何将 javascript 变量从 Rails 视图传递到操作
【发布时间】:2019-07-09 14:48:07
【问题描述】:

在我为我的操作创建的一些 Javascript 代码中,我有一个用户电子邮件列表。视图采用haml 格式。 我想按表单上的提交按钮并在同一操作或其他操作中访问此电子邮件列表。

我尝试过使用 form_tag、form_for 和普通的 html 表单,但是我一直遇到不同的问题。我还测试了 link_to 和 button_to 标签。

这是我们为 Action 准备的内容:

module RailsAdmin
  module Config
    module Actions
      class BulkProperties < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :collection do
          true
        end

        register_instance_option :http_methods do
          [:get, :put]
        end

        register_instance_option :controller do
          proc do
            @all_users = User.all
            @all_properties = UserProperty.all
            @all_roles = Role.all
            binding.pry
            if request.get? # EDIT
              respond_to do |format|
                format.html { render @action.template_name }
                format.js   { render @action.template_name, layout: false }
              end

            elsif request.put? # UPDATE
              binding.pry #Just testing this
          end
        end

        register_instance_option :link_icon do
          'icon-lock'
        end
      end
    end
  end
end

这是我的观点给我带来问题的部分:

%form
  .multiselect-form
    .wrapper{"data-children-count" => "1"}
      %select.collection{:multiple => "multiple", :id => "multi"}
  %hr/
  %h1 List of user properties
  -@all_properties.each do |prop|
    %input{:name => prop.name+"checkbox", :type => "checkbox", :id => prop.name+"checkbox"}
    #{prop.name}
    %input{:name => prop.name, :type => "text", :id => prop.name}
    %br
  %input{:name => "submit", :type => "submit", :formaction => 'http://localhost:3000/admin/user/bulk_properties?Application=8&Company=1&locale=en', :method => 'put'}
%br/
%br/

结果是我被引导到一个 CanCan 问题,我得到“你没有被授权......”。这是因为 url 实际上最终保存了参数,因此它试图将我重定向到一个不存在的网页。

【问题讨论】:

    标签: ruby-on-rails view action rails-admin


    【解决方案1】:

    我实现了一个将带有 js 的文件上传到 S3 并使用 javascript 设置表单字段的值的操作,然后我在 rails 管理操作上使用该 url。

    我将其全部粘贴,因为在过去,通过将我的方法与工作方法进行比较,在这些情况下会对我有所帮助。

    以下是相关(简化)文件:

    # app/views/rails_admin/main/update_orders_and_line_items.html.haml
    
    = form_tag(method: :post) do
      = hidden_field_tag 'uploaded-file-url', '', id: 'uploaded-file-url'
      = submit_tag 'Submit', class: 'action-button-pink'
    
    # app/assets/javascripts/google_cloud_storage_field.js 
    
    let url = get_url(fileInput);
    let valueField   = $('#uploaded-file-url');
    valueField.val(url);
    
    # lib/rails_admin/config/actions/update_orders_and_line_items.rb
    
    module RailsAdmin
      module Config
        module Actions
          class UpdateOrdersAndLineItems < RailsAdmin::Config::Actions::Base
            # Might cause random bugs if enabled
            register_instance_option :pjax? do
              false
            end
    
            register_instance_option :http_methods do
              [:post, :get]
            end
    
            register_instance_option :controller do
              proc do
                if request.post?
                  url = params['uploaded-file-url']
    
                  do_stuff_with(url)
                elsif request.get?
                  # [...]
                end
              end
            end
          end
        end
      end
    end
    

    我相信您的错误的来源是使用%form 标签,表单的默认方法是 GET,这就是为什么您的浏览器会尝试将您重定向到某个页面,如果您像我一样使用 form_tag(method: :post) 并制作确保电子邮件在 hidden_​​field_tag 值上,您将在 action.rb 中获得您的列表

    【讨论】:

    • 谢谢你,它让我走上了正轨,现在我的行​​动有了我的参数!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多