Ruby on Rails 7 - 无法打开与 localhost:9200 的 TCP 连接(连接被拒绝 - connect(2) for 127.0.0.1:9200)

问题描述 投票:0回答:1

我正在使用 WSL 和 Ubuntu 应用程序。 我正在使用 searchkick 和 elasticsearch gems。 我也安装了 TomSelect。 Ruby on Rails 7.

问题:

当我输入时:

sudo service elasticsearch start

我得到:

elasticsearch: unrecognized service

但是,当我转到联系人页面上的导航栏并尝试搜索联系人时,搜索效果很好。所以看起来elasticsearch是通过gemfile安装的。

但是当我进入联系人页面,单击“新联系人”并尝试提交该表单时,我不断收到此法拉第错误:

但是,有时,即使我收到该错误,某些联系人似乎最终还是被保存了。我不知道为什么或如何,但当我进入联系人索引时,一些新联系人实际上最终出现在那里。

我的联系人控制器如下所示:

class ContactsController < ApplicationController before_action :set_contact, only: %i[ show edit update destroy ] # GET /contacts or /contacts.json def index @contacts = current_user.contacts #Ransack variables for search @query = Contact.ransack(params[:q]) @results = @query.result(distinct: true) end # GET /contacts/1 or /contacts/1.json def show end # GET /contacts/new def new @contact = Contact.new end # GET /contacts/1/edit def edit end # POST /contacts or /contacts.json def create @contact = Contact.new(contact_params) @contact.user = current_user respond_to do |format| if @contact.save format.html { redirect_to contact_url(@contact), notice: "Contact was successfully created." } format.json { render :show, status: :created, location: @contact } else format.html { render :new, status: :unprocessable_entity } format.json { render json: @contact.errors, status: :unprocessable_entity } end end end # PATCH/PUT /contacts/1 or /contacts/1.json def update respond_to do |format| if @contact.update(contact_params) format.html { redirect_to contact_url(@contact), notice: "Contact was successfully updated." } format.json { render :show, status: :ok, location: @contact } else format.html { render :edit, status: :unprocessable_entity } format.json { render json: @contact.errors, status: :unprocessable_entity } end end end # DELETE /contacts/1 or /contacts/1.json def destroy @contact.destroy respond_to do |format| format.html { redirect_to contacts_url, notice: "Contact was successfully destroyed." } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_contact @contact = Contact.find(params[:id]) end # Only allow a list of trusted parameters through. def contact_params params.require(:contact).permit(:first_name, :last_name, :last_known_country, :last_known_city, :mobile_phone_1, :mobile_phone_2, :office_phone_1, :office_phone_2, :home_phone, :other_phone, :email_1, :email_2, :email_3, :email_4, :email_5, :website_1, :website_2, :website_3, :website_4, :website_5, :website_6, :website_7, :website_8, :contact_apps, :birthday, :address_1, :address_2, :how_we_met, :things_I_like, :best_memories, :areas_for_improvement, :notes, category_ids:[], contact_group_ids:[], contact_type_ids:[]) end end
为了添加上下文,有时我会收到法拉第错误,但有时,表单本身(对于新联系人)甚至不会提交。没有 JS 错误。网络选项卡中没有任何内容。 “提交”按钮似乎没有任何作用。

因此,我收到两个错误之一:1)法拉第错误,或 2)表单本身未提交(但应用程序上的所有其他表单都工作正常)。

谢谢您的指导!

编辑:添加了 contact.rb

class Contact < ApplicationRecord belongs_to :user has_many :interactions has_many :contact_categories, dependent: :destroy has_many :categories, through: :contact_categories has_many :contact_groupings, dependent: :destroy has_many :contact_groups, through: :contact_groupings has_many :contact_typings, dependent: :destroy has_many :contact_types, through: :contact_typings #Automatically adds each new contact as Recently Added after_create :assign_to_default_category def assign_to_default_category default_category = user.categories.find_by(id: 1) if default_category categories << default_category else # Handle the case where the default category with ID 1 is not found # You may want to log a warning or handle it in a way that fits your application end end # Uncomment to make the search bar work; but it keeps causing faraday issues on new contacts def self.ransackable_associations(auth_object = nil) ["category", "contact_groupings", "contact_groups", "contact_categories", "contact_types", "contact_typings", "user"] end def self.ransackable_attributes(auth_object = nil) ["address_1", "address_2", "areas_for_improvement", "best_memories", "birthday", "contact_apps", "created_at", "email_1", "email_2", "email_3", "email_4", "email_5", "first_name", "home_phone", "how_we_met", "id", "last_known_city", "last_known_country", "last_name", "mobile_phone_1", "mobile_phone_2", "notes", "office_phone_1", "office_phone_2", "other_phone", "points", "things_I_like", "updated_at", "user_id", "website_1", "website_2", "website_3", "website_4", "website_5", "website_6", "website_7", "website_8"] end searchkick text_middle: [:first_name, :last_name, :last_known_country, :last_known_city] end
    
elasticsearch ruby-on-rails-7 searchkick faraday
1个回答
0
投票
好的,所以你仍然可以搜索,因为你的搜索使用的是 Ransack,而不是 ES。您的

searchkick

 是正确的并且可以工作,但似乎您没有任何实际的 ES 服务/守护程序正在运行,因此您需要安装它。 
searchkick
 添加为 
after_commit
,因此不会影响数据库中记录的保存,这也是您看到新联系人的原因。

不知道为什么你的表单有时不提交,我首先查看我的 Rails 日志,看看服务器是否正在接收请求,如果没有,那么我会查看我的浏览器控制台以查看是否有错误当尝试提交时。如果您无法解决该问题,请作为一个单独的问题提出,包括您的发现以及您视图中的相关表单以及生成的 HTML。

© www.soinside.com 2019 - 2024. All rights reserved.