Would love it if anyone can see what I'm doing wrong. Followed the docs: https://github.com/activerecord-hackery/ransack
My model defines that a Signup has_many Inventories
Controller code:
def index
@q = Inventory.search(params[:q])
@inventories = @q.result.includes(:signup)
end
View code:
<%= search_form_for @q, url: url_for(controller: 'inventories', action: 'index') do |f| %>
<%= f.label :item_name_cont %>
<%= f.search_field :item_name_cont %>
<%= f.label :signup_email_cont %>
<%= f.search_field :signup_email_cont %>
<%= f.submit %>
<% end %>
<table>
<thead>
<tr>
<th><%= sort_link(@q, :item_name, "Item", default_order: :desc) %></th>
<th><%= sort_link(@q, 'signups.email', "Email") %></th>
<th>Action</th>
<th colspan="5"></th>
</tr>
</thead>
<tbody>
<% Inventory.all.each do |inventory| %>
<tr>
<td><%= inventory.item_name %></td>
<td><%= inventory.signup.email %> %></td>
</tr>
</tbody>
</table>
Also, if it's helpful, if I remove the url:
specification in the search form, I get an error: No route matches [GET] "/inventories/search"
Better Option
Please make sure that the view code that you posted is in views/inventories/index.html.erb
file and change Inventory.all.each
to @inventories.each
. Then you would be able to access the search form at http://localhost:3000/inventories
.
Or
From the error that you mentioned, it looks like you are doing this on /inventories/search
page. If you want to stick to that URL, move your index
method code into search
method in your controller (as shown below) and add a route for search
with GET in your routes file.
def search @q = Inventory.search(params[:q]) @inventories = @q.result.includes(:signup) end