我正在尝试为搜索结果添加csv,但是它会提示错误,提示不允许的参数。我应该在csv链接中传递哪些参数?这样,csv才能下载唯一的搜索结果。
ReportsController
def revenue_search
@bookings = Booking.complete.order("created_at DESC")
date = "1-#{params['date']['month']}-#{params['date']['year']}"
date = Date.parse(date)
@bookings = @bookings.where(created_at: date.beginning_of_month..date.end_of_month)
@per_page = params[:per_page] || Booking.per_page || 20
@bookings = @bookings.paginate( :per_page => @per_page, :page => params[:page])
if params[:currency].present?
@bookings = @bookings.where(currency: params[:currency])
end
respond_to do |format|
format.html
format.csv { send_data @bookings.revenue_csv }
end
end
型号
def self.revenue_csv
attributes = %w{ total value deposit balance }
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |booking|
csv << attributes.map{ |attr| booking.send(attr) }
end
end
end
revenue.html.erb
<%= form_tag revenue_search_path, :method => 'get' do %>
<%= select_tag(:currency, options_for_select(["USD", "GBP"]), class: "form-control custom-select" , :prompt => 'By Office' ) %>
<%= select_month((@selected_date || Date.today), {}, class:"custom-select", :prompt => 'By Month')%>
<%= select_year((@selected_year || Date.today.year), {}, class:"custom-select" )%>
<%= submit_tag "Search", name: nil , class: "btn btn-info" %>
<% end %>
<table id="demo-foo-addrow" class="table m-t-30 table-hover no-wrap contact-list no-paging footable-loaded footable" data-page-size="2">
<thead>
<tr>
<th class="footable-sortable">Total No of students<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Value of courses sold<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Total Deposits<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Balance payments<span class="footable-sort-indicator"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= @bookings.count %></td>
<td><%= @bookings.count %></td>
<td><%= @bookings.sum{|x| (x.payment_gross.to_f)} %></td>
<td><%= @bookings.sum{|x| (x.course&.course_fee || x.event&.course_fee).to_f - x.payment_gross.to_f }.to_f %></td>
</tr>
</tbody>
</table>
<p> Download: <%= link_to "CSV", revenue_search_path(format: "csv" , params: params ) %> </p>
日志:
ActionView::Template::Error (unable to convert unpermitted parameters to hash):
42: </div>
43: </div>
44: </div>
45: <p> Download: <%= link_to "CSV", revenue_search_path(format: "csv" , params: params ) %> </p>
app/views/reports/revenue_search.html.erb:45:in `_app_views_reports_revenue_search_html_erb___71501213_135144540'
答案 0 :(得分:1)
您可以使用to_unsafe_h
revenue_search_path(format: "csv" , params: params.to_unsafe_h)