Reputation: 5953
In a Rails 3 project, I have an index list of costprojects
.
I have a controller action called `viewprojects'.
I would like to add a checkbox column to the index list. Then let the user select which projects should be included in the pdf.
index:
<% @costprojects.each do |costproject| %>
<tr>
<td><%= check_box_tag "costproject_ids[]", costproject.id %></td>
...
<%= link_to 'PDF Selected', costprojects_viewprojects_path(:format => "pdf",:costproject_ids => costproject_ids[] ), :class => 'btn btn-success', :target => "_blank" %>
Controller:
def viewprojects
@costprojects = params[:costproject_ids]
respond_to do |format|
format.html
format.pdf do
render :pdf => "costprojects.pdf",
:show_as_html => params[:debug].present?,
:page_size => 'letter'
end
end
end
I'm getting:
undefined local variable or method `costproject_ids'
Update1:
Tried:
def viewprojects
@costprojects = params[:costproject_ids[]]
...
<%= link_to 'PDF Selected', costprojects_viewprojects_path(:format => "pdf",:costproject_ids[] => costproject_ids[] ), :class => 'btn btn-success', :target => "_blank" %>
I get "wrong number of arguments (0 for 1..2)"
Do I need to add form_tag and submit_tag?
Thanks for the help!
Upvotes: 1
Views: 902
Reputation: 11072
As Legendary mentioned in the comments you need to wrap this in a form. A link_to
isn't going to transmit the value of the selected ids unless its contained in its query parameters, which it can't know at render time.
Try something more like this:
<%= form_tag costprojects_viewprojects_path(format: "pdf") do %>
<% @costprojects.each do |costproject| %>
<tr>
<td><%= check_box_tag "costproject_ids[]", costproject.id %>
</td>
<% end %>
...
<%= submit_tag "PDF Selected", class: "btn btn-success" %>
<% end %>
You should then be able to access the array of cost_project_ids
simply with params[:cost_project_ids]
in the controller, which it seems you're already doing. Note that this will send an HTTP POST, rather than a GET, so ensure your routes are correctly setup for that verb.
Upvotes: 2