I have a simple select form. When a year is selected from the form I would like the results to be returned to the same page but below the form. Can someone explain how to do this? Here is the form page (index.html.erb)
<%= form_tag("lookup/show", :method => "get") do %>
<%= label_tag(:q, "Pub Year :") %>
<%= collection_select(:lookup, :pubyear, @pubyears, :pubyear, :pubyear) %>
<%= submit_tag("Find") %>
<% end %>
Here is the show method from the Lookup controller
def show
@lookuprows = Lookup.return_lookup_row(params[:lookup][pubyear])
respond_to do |format|
format.html
end
end
Here is the show.html.erb page that the results currently go to
<tbody class="lkuptbody">
<% @lookuprows.each do |lkup| %>
<tr class="lkuprow">
<td><input type="text" class="lkupcode" value=<%= lkup.codetype %> /></td>
<td><input type="text" class="lkupdesc" value=<%= lkup.codedesc %> /></td>
<td><input type="text" class="lkuprmks" value=<%= lkup.rermark %> /></td>
</tr>
</tbody>
I understand that I will have to make a partial _show.html.erb, but how do I reference that from the form page (index.html.erb)?
Thanks
If you want the results to appear on the same page but below the form, then the form should send the results to the
indexaction, not to theshowaction:and in your LookupController:
Then just append the table HTML in your show page below the form (in index.html.erb) wrapped in an
ifblock to filter out the case where@lookuprowsis nil:This will show the results in
@lookuprowsas a table if there are any, if not it will not show the table.You may want to put that table HTML in a separate partial to clean up the view, but that is not essential to the problem you asked.
Hope that helps.