Rails:将Mongoid文档添加到Temp Store以在文件导出表中使用

问题描述 投票:0回答:1

我试图允许用户从表中选择行并将每个记录添加到“导出列表”。此导出列表是另一个显示用户选择的记录的表。然后有一个导出到文件的选项..

我被困在获取用户选择的文档ID并将其显示在单独的表中。

我有这个设置:

路线:

Rails.application.routes.draw do
  resources :scenarios do
    collection do
      get :call_copy
      get :export
    end
  end
end

场景控制器:

class ScenariosController < ApplicationController
  before_action :set_scenario, only: [:show, :edit, :update, :destroy]
  before_action :all_scenarios, only: [:index, :create, :update]
  respond_to :html, :js
  ...
  def index
    @scenarios = if params[:submitter].blank? && params[:application].blank? && params[:pillar].blank? && params[:test_type].blank? && params[:begin_date].blank? && params[:end_date].blank? && params[:search_text].blank?
               Scenario.all.order_by(created_at: :desc)
             else
               Scenario.search_text(params)
             end
    respond_to do |format|
      format.html
      format.js
    end
  end
  ...
  def export
    @scenario = Scenario.find(params[:id])
    @export_scenarios ||= []
    @export_scenarios << @scenario

    respond_to do |format|
      format.html
      format.js
    end
  end
  ...
end

index.js.erb的:

$('#export_class').html("<%= j (render 'export', export_scenarios: @export_scenarios) %>")

Index.html.erb:

<Table>
...
    <li><%= link_to 'Export', export_scenarios_path(id: scenario), remote: true %></li>
...
</table>

<div id="export_class">
  <%= render 'export', export_scenarios: @export_scenarios %>
</div>

_Export.js.erb:

$('#export_class').html("<%= j (render 'export', export_scenarios: @export_scenarios) %>")

_Export.html.erb:

<table class="table table-striped" style="max-height: 800px; overflow: scroll;">
  <thead>
  <tr>
    <th>Scenario Name</th>
    <th>Scenario Body</th>
    <th>Options</th>
    <th colspan="8"></th>
  </tr>
  </thead>
  <tbody>
  <% export_scenarios.each do |scenario| %>
    <tr>
      <td class="text-left"><%= scenario.scenario_name %></td>
      <td class="text-left"><%= scenario.scenario_body %></td>
      <td>
        <button class="btn btn-danger dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
          <i class="fa fa-times"></i>
        </button>
      </td>
    </tr>
  <% end %>
  </tbody>
</table>

<%= submit_tag "Export!", type: "submit", class: "btn btn-primary pull-right", :name => nil %>
ruby-on-rails ajax ruby-on-rails-5
1个回答
0
投票

尝试传递方案:

render partial: 'export', locals: {export_scenarios: @export_scenarios}

请注意locals键(编辑:和partial)。

© www.soinside.com 2019 - 2024. All rights reserved.