volt
volt

Reputation: 357

Params to controller

In my project I got a view, in this view I have a date input like this:

<%= form_tag installations_path, :method => :csv, :class => 'form-search' do %>
<%= select_date(date = Date.current, options = {}, html_options = {}) %>
<%= submit_tag "Save" %>
<% end %>

In my controller Installation I got a method with the name "csv" this method generate a Csv. Example:

def csv
  @consumption = current_user.get_consumptions(params[:year])
  respond_to do |format|
      format.html
      format.csv { send_data @consumption.to_csv }
  end
end

When I execute the view I receive this error:

param is missing or the value is empty: installation

The problem is that in my installations_params I have require(:installation) cause I use object installation in others methods.

def installation_params
    params.require(:installation).permit(:year,...)
end

What is the best way to resolve this problem ?

Upvotes: 0

Views: 57

Answers (1)

dinglixiang
dinglixiang

Reputation: 31

  1. The request log may help you firstly.
  2. If you generate the csv as a GET request, then change the code

    from:

    <%= form_tag installations_path, :method => :csv, :class => 'form-search' do %>
    

    to:

    <%= form_tag csv_installations_path, :method => :get, :class => 'form-search' do %>
    

    Make sure the csv_installations_path is exist.

  3. Then, you can get the year params as params[:year] if your input named year.

  4. At last, You don't use any install_params in csv method

Hope it helps you.

Upvotes: 1

Related Questions