james
james

Reputation: 4049

Ransack not working (no error, just not functioning)

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"

Upvotes: 0

Views: 3377

Answers (3)

Harry Fairbanks
Harry Fairbanks

Reputation: 408

I think I fell into the same situation. It might be working it's just tricky to identify. Are you getting any returns? Possible all the returns? If that is the case it might be a matter of the default search. The default search returns everything as you would expect if you had just put Inventory.all Try .

Inventory.ransack(name_eq: 'potatos').result

this would also work

Inventory.ransack(special_potato_eq: 'potatoes').result

This will limit the return to exact matches of Inventory.potato or Inventory.special_potato

This is the exact bit of code that worked for me

@q = User.ransack(email_eq: params[:q][:email])

Checkout the reference link for other search options.

Reference: https://github.com/activerecord-hackery/ransack/wiki/basic-searching

Upvotes: 0

San
San

Reputation: 1954

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

Upvotes: 1

acacia
acacia

Reputation: 1387

Change this;

def index
  @q = Inventory.search(params[:q])
  @inventories = @q.result.includes(:signup)
end

to

def index
  @q = Inventory.ransack(params[:q])
  @inventories = @q.result.includes(:signup)
end

Upvotes: 0

Related Questions