skip to Main Content

I am using WSL and the Ubuntu app.
I am using the searchkick and elasticsearch gems.
I have TomSelect installed as well.
Ruby on Rails 7.

ISSUES:

When I type: sudo service elasticsearch start

I get: elasticsearch: unrecognized service

However, when I go to the navbar on the contacts page and try to search for a contact, the search works fine. So it seems elasticsearch is installed via the gemfile.

But when I go to the contacts page, and click ‘new contact’ and try to submit that form, I keep getting this Faraday error:

Error Message I am Getting

However, sometimes, even though I get that error, some of the contacts appear to end up being saved. I’m not sure why or how, but when I got to the contacts index, some of the new contacts actually end up there.

My contacts controller looks like this:

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

For added context, sometimes I get that Faraday error, but at other times, the form itself (for a new contact) doesn’t even submit. No JS errors. Nothing in the network tab. The ‘submit’ button just doesn’t seem to do anything.

So I’m getting one of two errors: 1) Faraday error, or 2) the form itself doesn’t submit (yet every other form on the application works fine).

Thank you for any guidance!

EDIT: Added 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

2

Answers


  1. Chosen as BEST ANSWER

    I finally discovered that the reason I am getting this error is... because elasticsearch was not installed.

    The solution is here: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-elasticsearch-on-ubuntu-20-04

    That includes the commands on how to install elasticsearch on Ubuntu.

    Note that I received the following error as follows after running the first command:

    /mnt/c/coding/contactpoints (get/form-to-submit) $ curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

    Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).

    ChatGPT Answer:

    It looks like you've added the GPG key for Elasticsearch successfully. The warning about apt-key being deprecated is informational and suggests that you manage keyring files in trusted.gpg.d instead, which is a newer and more secure way to handle GPG keys.

    Now, you can proceed with adding the Elasticsearch APT repository to your system. After adding the GPG key, you typically need to add the repository to your apt sources. The following commands should do that for you:

    sudo sh -c 'echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" > /etc/apt/sources.list.d/elastic-7.x.list'
    

    This command adds the Elasticsearch APT repository for version 7.x to your system.

    After adding the repository, you should update the package list and install Elasticsearch:

    sudo apt-get update
    sudo apt-get install elasticsearch
    

    Finally, start the Elasticsearch service:

    sudo service elasticsearch start
    

    This should start Elasticsearch on your machine. Remember to check the status to ensure it's running:

    sudo service elasticsearch status
    

    END CHAT GPT ANSWER

    I finally got it to work.


  2. Ok, so you can still search because your search is using Ransack, not ES. Your searchkick is correct and would work but it seems you don’t have any actual ES service/daemon running, so you’d need to install that. searchkick is added as an after_commit so wouldn’t impact the saving of your record in your DB, hence why you see new contacts.

    No idea why your form sometimes doesn’t submit, I’d start by looking in my Rails log, see if the server is receiving the request, if not then I’d look at my browser console to see if there’s an error there when trying to submit. If you can’t resolve that then ask it as a separate question, including your findings and the relevant form from your view and the generated HTML.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search