1

This is my routes.rb

namespace :api do
    get 'suggestions/jobs', to: "suggestions#jobs"
end

My controller

class Api::SuggestionsController < ApplicationController
    def jobs
        @jobs = Job.job_title_search(params[:q])   #.select(:title, :label).distinct
        if @jobs.present?
            render json: @jobs, status: :ok
        else
            render json: "Not Found", status: :ok
        end
    end
end

and model

def self.job_title_search(q)
    where('title LIKE ?', "%#{q}%").select(:title, :label).distinct
end

in the development environment like localhost:3000/api/suggestions/jobs?q=dev the data sample is

[
    {"id":null,"title":"React Native Developer","label":"Top label job"},
    {"id":null,"title":"Android Developer","label":"Top label job"},
    {"id":null,"title":"Business Development Representative","label":"Mid label job"},
    {"id":null,"title":"Node.js Developer","label":"Top label job"}
]

that means it's working, but while I pushed into the Heroku then like example.herokuapp.com/api/suggestions/jobs?q=dev it's showing

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I know there is "Not Found" like the code is

render json: "Not Found", status: :ok

My question is why same code is not working on Heroku? and What can I do for this?

the database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: my_project_production
  username: my_project
  password: <%= ENV['MY_PROJECT_DATABASE_PASSWORD'] %>

Any help appriciated.

Thanks

jesica
  • 645
  • 2
  • 13
  • 36
  • Could you please provide the `database.yml` for checking the database adapter, I think it's causing for database adapter, the `like` does not work in `PostgreSQL` OTOH, `ilike` does not work in `SQLite`, if you use `SQLite` for development & `PostgreSQL` for production, – fool-dev May 23 '18 at 05:27
  • @fool-dev I have updated the question. – jesica May 23 '18 at 05:38

2 Answers2

6

The if branch of your code is fine, and since dev contains records, it’s being rendered without any glitch.

In prod, OTOH, there are no records and the else branch, which is not quite correct, is tried to be rendered. And it fails, giving you the error.

Do supply a hash there instead:

else
  render json: {error: "Not Found"}, status: :ok
end
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
1

Look the LIKE keyword in SQL, if you use SQLite database for development and PostgreSQL database for production then maybe it's happened, the like is working on the development environment and production environment will be ilike the like does not work, OTOH, ilike does not work in SQLite.

If so then the solution is:

Solution 1 You can change the development database to PostgreSQL, if you have worry about changing the database then follow the below

Solution 2 You can use lower(attr) like

def self.job_title_search(q)
    where('lower(title) LIKE lower(?)', "%#{q}%").select(:title, :label).distinct
end

You can see the SO Answer for this.

Hope will help.

fool-dev
  • 7,671
  • 9
  • 40
  • 54