Я пытаюсь разрешить пользователю выбирать строки из таблицы и добавлять каждую запись в «список экспорта».Этот список экспорта является еще одной таблицей, которая отображает записи, выбранные пользователем.Затем есть возможность экспортировать в файл.
Я застрял при получении идентификатора документа, выбранного пользователем, и отображении его в отдельной таблице.
У меня есть такая настройка:
Маршруты:
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 %>