I've ran into a bit of a problem. I watched Railscasts 370
and discovered a cool new gem called ransack
My problem is it's not doing what I want it to do, which is to filter a table based on a search
Here is my controller
def index
@search = Component.ransack(params[:q])
@components = @search.result
end
And my view
<%= search_form_for @search do |f| %>
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>
<%= f.submit "Search" %>
<% end %>
When I typed in "Gigabyte" and clicked search, I saw the following in the URL
http://localhost:3000/components?utf8=%E2%9C%93&q%5Bname_cont%5D=Gigabyte&commit=Search
I know that this is URL syntax for a query. And it refreshes the page as well. Problem is, I see the following three results
Intel Core i5-4460 3.2GHz Quad-Core
Gigabyte GA-Z97X-SLI ATX LGA1150
Team Elite Plus 8GB (2 x 4GB) DDR3-1600 Memory
These were the names of the 3 items in my table. I expected only one result to return from this query.
If it helps, I've added part of my Development Logs
Processing by ComponentsController#index as HTML
Paramaters: {"utf8=>"checkmark", "q" => {"name_cont"=>"Gigabyte"}, "commit"=>"Search"
It shows up in the parameters too, so I'm completely mystified as to why my code's not working. Any help will be greatly appreciated.
EDIT
I think I found the problem but I'm not sure
I have this inside <tbody>
<tbody>
<% Component.all.each do |c| %>
<tr>
<td> <strong><%= c.part %></strong> </td>
<td> <a id="links" href="<%=c.link%>"> <%= c.name %> </a></td>
<td> $<%= c.price %> </td>
</tr>
<% end %>
</tbody>
EDIT
Looks like that was indeed my mistake. Changing Component.all
to @components.each
fixed it.
Have you tried to change this
@search = Component.ransack(params[:q])
for this?:
@search = Component.search(params[:q])
I've been using the gem recently and probably the gem changed a little with regard the railscast