5

I was trying to add csv for my the search results but it throws error telling unpermitted params. What params should i pass in the csv link? So that csv would download the only the search results.

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

Model

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>

logs:

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'
Penny
  • 157
  • 3
  • 17
  • Can you update the question with full error message and the log that shows where the error is? – Pavan Sep 18 '18 at 07:53
  • @Pavan i have added the logs plz check. – Penny Sep 18 '18 at 07:59
  • The accepted answer here has a bunch of suggestions: https://stackoverflow.com/questions/46029084/rails-unable-to-convert-unpermitted-parameters-to-hash?rq=1 - essentially newer rails version throw an error if you try to use params-object as a hash. I guess the easiest way for you is to think about which params you want to pass to your download-link and filter them with params.permit(..) – trueunlessfalse Sep 18 '18 at 08:02
  • `params: params` doesn't make any sense. What are you trying to do? Shouldn't it be just `<%= link_to "CSV", revenue_search_path(format: "csv" )` %> – Pavan Sep 18 '18 at 08:03
  • @Pavan when i try this <%= link_to "CSV", revenue_search_path(format: "csv" ) %> it throws error telling undefined method `[]' for nil:NilClass for controller code date = "1-#{params['date']['month']}-#{params['date']['year']}" – Penny Sep 18 '18 at 08:06
  • @Pavan sending only csv will show all the results , it should download only the search results, so we need to pass the params rite – Penny Sep 18 '18 at 08:07
  • @trueunlessfalse ok thanks il look into the link and try – Penny Sep 18 '18 at 08:08
  • @SushmaKv I agree but you cannot pass the `params` like this through the `link_to` if you want to send dynamic values of search results to the `revenue_search` method. You require a different approach. – Pavan Sep 18 '18 at 08:14
  • @Pavan so what is that approach so that i would get those results in the csv – Penny Sep 18 '18 at 08:21
  • Without testing I think revenue_search_path(params.permit(:query).merge(format: 'csv')) could work. (where you should replace :query with the params you actually need) – trueunlessfalse Sep 18 '18 at 08:32
  • @trueunlessfalse that solves the error but i am still not able to download the csv. it tells undefined method `[]' for nil:NilClass for this line in revenue_search action date = "1-#{params['date']['month']}-#{params['date']['year']}" – Penny Sep 18 '18 at 08:37
  • 1
    If you deal with nested parameters you need to specify that for permit, In you case something like that should work: params.permit(date: [:year, :month]) – trueunlessfalse Sep 18 '18 at 08:40
  • @trueunlessfalse thanks it worked by adding permit – Penny Sep 18 '18 at 11:08

1 Answers1

6

You can use to_unsafe_h

revenue_search_path(format: "csv" , params: params.to_unsafe_h)
mbajur
  • 4,406
  • 5
  • 49
  • 79
thaleshcv
  • 752
  • 3
  • 7