Home>

I'm a rails beginner and I'm developing an original app.
When I implemented it using ransack's gem to introduce the search function, an error occurred.
When I press the search button, the show method works instead of the search method for some reason, and I don't know the cause.
There is an items table and a category table, and they form an association.
This time, I am implementing it because I want to output search results for each category of posted items.
Thank you.

ActiveRecord :: RecordNotFound in ItemsController # show
Couldn't find Item with'id' = search
Extracted source (around line # 69):
  def set_item
    @item = Item.find (params [: id])
  end
Corresponding source code

apps/contorollers/items_controller.rb

class ItemsController</pre>
<p><br />
index.html.erb</p>
<pre><code><% = search_form_for @search, url: items_search_path do | f |%>
<% = f.label: category_name_eq,'Category:'%>
<% = f.collection_select: category_name_eq, @category_name,: name,: name%>
<br>
<% = f.submit'Search'%>
<% end%>

routes.rb

Rails.application.routes.draw do
  root to: "items # index"
  devise_for: users
  resources: items
  get'items/search'
end
What I tried

I wrote it in the search method using binding.pry, but it doesn't stop, so it seems that the search method is not working well.
Thank you.

Supplementary information (FW/tool version, etc.)
  • Answer # 1

    I managed to solve it on my own.
    Since the show method will inevitably work,
    <% = search_form_for (@search, url: items_search_path, method :: search) do | f |%>
    I specified the method in.
    Then I got a Routing Error this time, so I solved it easily.
    I felt like I was forced to do it, but it's a solution.
    However, if anyone knows a solution that is not this solution, we are still looking for answers, so thank you.