Home>
I want to achieve

Get the value entered in the form and display the search results

After entering a value in the form, I want to call the search action of tasks_controller, but the show action is called.

Corresponding source code

version
Rails 5.2.4.2

index.html.erb

<tbody>            <tr>              <td>                <input type = "form" name = "title">              </td>              <td>                <select name = "to_user_id">                  <option value selected>-</option>                  <option value = "#"></option>                </select>              </td>              <td>                <input type = "form" value name = "due_date">              </td>            </tr>            <% @ tasks.each do | task |%>              <tr>                <td>                                          <% = link_to task.task_name, task_path (task.task_name)%>                                      </td>                <td>                                        <span>sample name</span>                </td>                <td>                                            Start at<% = task.created_at.strftime ('% Y-% m-% d')%>                                      </td>                <td>                  <% = link_to edit_task_path (task), class: "btn btn-primary ajax-link tip" do%>                    <i></i>                  <% end%>                  <% = link_to task_path (task), method:: delete, data: {confirm: "Are you sure I want to delete?<br><strong># {task.task_name}</strong>", cancel:'Cancel' , commit:'delete'},

 title:'Confirm Delete', class: "btn btn-danger tip" do%>                  <i></i>                  <% end%>                     record                   </td>              </tr><% end%>            </tbody>

tasks_controller.rb

class TasksController</pre>
<p>task.js</p>
<pre><code>$(document) .on ('keyup','#search', function (e) {
  e.preventDefault ();
  var input = $.trim ($(this) .val ());
  console.log (input)
  $.ajax ({{
    url:'/ tasks/search',
    type:'GET',
    data: ('keyword ='+ input),
    processData: false,
    contentType: false,
    dataType:'json'
  })
  .done (function (data) {
    $('tr.tasks_column-data'). remove ();
    $(data) .each (function (index, task) {$('# result'). append (
        `<tr>      <td>                         ${task.task_name}                     </td>      <td>                    <span>sample name</span>      </td>      <td>                      <p>Start at ${task.created_at.strftime ('% Y-% m-% d')}</p>                  </td>      <td>                      <i></i>                     <strong>${task.task_name}</strong>"data-cancel =" Cancel "data-commit =" Delete "title =" Confirm deletion "rel =" nofollow "data-method =" delete "href ="/tasks/${task.task_users_id} ">          <i></i>                       record         </td>    </tr>`
      )
    });
  })
});

index.json.jbuilder

json.array! @tasks do | task |
  json.task_name task.task_name
  json.task_users_id task.task_users_id
end

Terminal log

Started GET "/ tasks/search? Keyword = aaaaa" for :: 1 at 2020-09-25 00:36:40 +0900
Processing by TasksController # show as JSON
  Parameters: {"keyword" =>"aaaaa", "id" =>"search"}
  Rendering tasks/show.html.erb within layouts/application
  Rendered tasks/show.html.erb within layouts/application (0.5ms)
  Rendered layouts/_navbar.html.erb (2.3ms)
  Rendered layouts/_sidebar.html.erb (2.2ms)
  Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 475ms (Views: 466.8ms | ActiveRecord: 0.0ms)
What I tried

-Removed show action from controller (because I didn't write anything)
・ Check if there are any mistakes in the description
-Use complete.log () to see how far the data is moving

I looked it up in Terratail, but I couldn't solve it.
I would appreciate it if anyone could teach me.