Link_to suggestions to solve an issue

Hi everyone, I have a show.html.erb that has a button to download a csv file of all the entries to a has-many model related to the main table in the show (villages). But I’m getting an error: “Couldn’t find Village without an ID” It has me stumped - the link_to is:

 <%= link_to 'Download Residents', download_residents_csv_villages_path(@village, format: :csv), class: 'btn btn-danger' %>

And the start of the def in the controller is:

def download_residents_csv
    @village = Village.find(params[:id])
    residents = @village.residents

Any thoughts appreciated? TIA, Dave

Somehow params[:id] has ended up being nil. Would be great to see if @village had ended up with an object back in the view template … change that line to be:

<%= puts @village.inspect
    link_to 'Download Residents', download_residents_csv_villages_path(@village, format: :csv), class: 'btn btn-danger' %>

And I bet the puts will write out nil to the terminal window right before the error message. Or perhaps there’s something interesting going on with your route for download_residents_csv_villages.

I would like to see what this looks like in your route.rb file you can try to prepend ‘id’ as a key <%= link_to 'Download Residents', download_residents_csv_villages_path(id: @village.id, format: :csv), class: 'btn btn-danger' %>