Ruby on Railsで初めてアプリを作っている初学者です。ajax通信ができず困っています。
jQueryを使って下記の通り作成しました。発火は起きていますが、controllerへ送られていないような感じです。
解決策見つからず、ご教示のほど、お願いいたします。
search.js
$(function() {
var search_list = $("contents.row");
function appendCompany(company) {
$(".search-form").on("submit", function() {
var input = $(".search-input").val();
console.log(input);
$.ajax( {
type: 'get',
url: '/searches',
data: { keyword: input },
dataType: 'json'
})
.done(function(companies) {
})
.fail(function() {
console.log("失敗です");
});
});
});
searches_controller.rb
def search
@companies = Company.where("name LIKE ?", "%#{params[:keyword]}%")
respond_to do |format|
format.html
format.json
end
end
index.html.erb
<div class="search-form">
<%= form_tag search_user_searches_path(current_user), :method => 'get' do %>
<%= text_field_tag :keyword, "", placeholder: "投稿を検索する", class: "search-input" %>
<%= submit_tag 'Search', class: 'search_btn' %>
<% end %>
</div>
rails routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
root GET / searches#index
user_companies GET /users/:user_id/companies(.:format) companies#index
edit_user_companies GET /users/:user_id/companies/edit(.:format) companies#edit
PATCH /users/:user_id/companies(.:format) companies#update
PUT /users/:user_id/companies(.:format) companies#update
user_items GET /users/:user_id/items(.:format) items#index
search_user_searches GET /users/:user_id/searches/search(.:format) searches#search
user_searches GET /users/:user_id/searches(.:format) searches#index
POST /users/:user_id/searches(.:format) searches#create
new_user_search GET /users/:user_id/searches/new(.:format) searches#new
edit_user GET /users/:id/edit(.:format) users#edit
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update