Elasticsearch & Bonsai: IndexMissingException on Heroku, works fine locally - ruby-on-rails

Building my first rails app and having issues getting Elasticsearch to run on Heroku.
Local works perfect.
On Heroku I can open the search page, but when I try to perform a search I get the error below:
Heroku logs show:
2015-05-18T12:05:15.969673+00:00 app[web.1]: ActionView::Template::Error ([404] {"error":"IndexMissingException[[homes] missing]","status":404}):
2015-05-18T12:05:15.969674+00:00 app[web.1]: 9: <% end %>
2015-05-18T12:05:15.969676+00:00 app[web.1]: 10:
2015-05-18T12:05:15.969677+00:00 app[web.1]: 11: <ul>
2015-05-18T12:05:15.969678+00:00 app[web.1]: 12: <% #homes.each do |home| %>
2015-05-18T12:05:15.969679+00:00 app[web.1]: 13: <li>
2015-05-18T12:05:15.969680+00:00 app[web.1]: 14: <h3>
2015-05-18T12:05:15.969682+00:00 app[web.1]: 15: <%= link_to home.name, controller: "homes", action: "show", id: home._id%>
2015-05-18T12:05:15.969683+00:00 app[web.1]: app/views/search/search.html.erb:12:in `_app_views_search_search_html_erb___1261310385184340853_69929402195100'
Gems:
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'bonsai-elasticsearch-rails', '~> 0.0.4'
Search Controller:
class SearchController < ApplicationController
def search
if params[:q].nil?
#homes = []
else
#homes = Home.search params[:q]
end
end
end
Search View:
<h1>Homes Search</h1>
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Go", name: nil %>
</p>
<% end %>
<ul>
<% #homes.each do |home| %>
<li>
<h3>
<%= link_to home.name, controller: "homes", action: "show", id: home._id%>
</h3>
</li>
<% end %>
</ul>
Any suggestions would be greatly appreciated!

The problem is similar to this issue.
The solution involves
running the tasks highlighted here
making sure the elasticsearch.rake exists
and finally if the 404 still happens to create the index manually using curl -XPOST http://account.region.bonsai.io/homes

Related

Template::Error undefined method `[]' for nil:NilClass

Hello I have a Rails application in production, and recently i started getting totally random errors on the loading of the show method of my "complaints" controller .
My "complaints" table has a one-to-one relationship with "interactions" table, you can create an interaction then create a complaint.
I display my data in a table on complaints#index, then I click on a row to get to complaints#show
I get this error :
ActionView::Template::Error (undefined method `[]' for nil:NilClass):
7: <div class="uk-width-expand#m">
8: <h1 class="uk-heading-primary">
9: <%# <%= link_to 'Retour à l\'interaction', interaction_path(#variables[:complaint].interaction, search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
10: <%= I18n.t("kind.#{#variables[:complaint].kind}") %>
11: <% if #variables[:complaint].interaction.date.present? %>
12: - <%= #variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
13: <% end %>
app/views/complaints/show.html.erb:10:in `_app_views_complaints_show_html_erb__763547916_154685640'
But this error appears totaly randomly. That means if I refresh the page, it may launch this time. So because of this, i can't understand where this error comes from, most of the time there is no error, but I get complaints from the app users saying they are really bothered.
Here is my controller show method :
def show
complaint = Complaint.find_by(id: params[:id])
if complaint.blank?
redirect_to complaints_path
return
end
interaction = Interaction.find(complaint.interaction.id)
result = DetailCommande.execute_procedure "p_detail_commande", interaction.do_piece, interaction.do_type
contenu_commande = DetailCommande.execute_procedure "p_contenu_commande", interaction.do_piece, interaction.do_type
#variables = { complaint: complaint, detail_commande: result[0], contenu_commande: contenu_commande }
#params = { search: params[:search] }
end
Here is the part of my complaints#show view that causes trouble :
<div class="uk-flex-middle uk-margin-small" uk-grid>
<div class="uk-width-expand#m">
<h1 class="uk-heading-primary">
<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
<%= I18n.t("kind.#{#variables[:complaint].kind}") %>
<% if #variables[:complaint].interaction.date.present? %>
- <%= #variables[:complaint].interaction.date.strftime('%d/%m/%Y') %>
<% end %>
</h1>
</div>
<div class="uk-width-auto#m">
<% if #variables[:complaint].control.present? %>
<button class="uk-button uk-button-default" disabled>Contrôle</button>
<% else %>
<a href="#modal-control-<%= params[:id] %>" class="uk-button uk-button-warning" uk-toggle>Contrôle</a>
<% end %>
<%= link_to 'Modifier', edit_complaint_path(id: #variables[:complaint].id, search: #params[:search]), class: 'uk-button uk-button-primary' %>
<%= link_to 'Supprimer', complaint_path(#variables[:complaint], search: #params[:search]), method: :delete, data: {confirm: 'Êtes-vous sûr ?'}, class: 'uk-button uk-button-danger' %>
</div>
</div>
And here is a screen of my page when it actually loads. Just simple data display
I don't know if i provided enough informations, but thank you in advance for your help
I think that your application is running on 2 servers , sharing the load by load balancer.
I think so because of the following 2 cases:
See line no. 9 of your log i.e.
9: <%# <%= link_to 'Retour à l\'interaction', interaction_path(#variables[:complaint].interaction, search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
and your html file does not contains this code, this are some changes in show.html i.e.
<%= link_to 'Retour à l\'interaction', interaction_path(id: params[:id], search: #params[:search]), class: 'uk-button uk-button-default uk-margin-right' %>
This error gone on page refresh, it means that 1 server contains correct code and 1 contains wrong code, so when the request goes on server 1, it displays the page and when it goes on server2, it gives error.
As per the logs, I think that complaint.kind is empty.

trouble using an api on heroku from a rails app

for a project at school I am trying to use the Bandsintown api to display upcoming concerts for artists that a user follows on my site. I am able to use the api the way I want to in development but when it comes to my heroku site in production nothing displays and I receive no errors, in fact I just receive an empty array. Perhaps you need permission from Bandsintown to use their api on heroku? I tried contacted them but have not heard a response yet.
My question, why am I able to get a response from Bandsintown on development but not in production? New to web dev in general, so I haven't come across this before..
Here is my code:
calendar_controller.rb
class CalendarController < ApplicationController
require 'uri'
def index
#user = current_user
#hash_version_array = []
#user.follows.each do |follow|
response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=#{app_id}&location=use_geoip")
#hash_version = JSON.parse(response.body)
#hash_version_array << #hash_version if #hash_version != []
end
#hash_version_array
end
end
calendar/index.html.erb
<h1><%= #user.name %>'s Calendar</h1>
<div>
<h4 class="inline"><%= link_to "Calendar" %></h4>
<h4 class="inline"><%= link_to "List" %></h4>
<h4 class="inline"><%= link_to "Map" %></h4>
</div>
<br>
<% #hash_version_array.each do |date| %>
<h3 class="artist_name"><%= date[0]["formatted_datetime"] %></h3>
<% date[0]["artists"].each do |info| %>
<h1 class="artist_name"><%= info["name"] %></h1>
<br>
<%= image_tag("#{info['thumb_url']}", class: 'artist_img') %>
<% end %>
<br>
<br>
<%= date[0]["venue"]["name"] %>
<br>
<%= date[0]["formatted_location"] %>, <%= date[0]["venue"]["country"] %>
<br>
Tickets <%= date[0]["ticket_status"].titleize %> <%= link_to("Buy", "#{date[0]['ticket_url']}") %>
<br>
<hr class="artists_hr">
<% end %>
Response from Bandsintown while working in development:
From: /Users/jasonquaccia/Sites/code/projects/capstone/the-music-project/app/controllers/calendar_controller.rb # line 15 CalendarController#index:
4: def index
5: #user = current_user
6: #hash_version_array = []
7:
8: #user.follows.each do |follow|
9: response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=el_proyecto_de_la_musica&location=use_geoip")
10: #hash_version = JSON.parse(response.body)
11:
12: #hash_version_array << #hash_version if #hash_version != []
13: end
14:
=> 15: binding.pry
16: #hash_version_array
17: end
[1] pry(#<CalendarController>)> #hash_version_array
=> [[{"id"=>10847723,
"title"=>"Metallica # AT & T Park in San Francisco, CA",
"datetime"=>"2016-02-06T19:00:00",
"formatted_datetime"=>"Saturday, February 6, 2016 at 7:00PM",
"formatted_location"=>"San Francisco, CA",
"ticket_url"=>"http://www.bandsintown.com/event/10847723/buy_tickets?app_id=el_proyecto_de_la_musica&artist=Metallica&came_from=67",
"ticket_type"=>"Tickets",
"ticket_status"=>"available",
"on_sale_datetime"=>"2015-11-06T10:00:00",
"facebook_rsvp_url"=>"http://www.bandsintown.com/event/10847723?app_id=el_proyecto_de_la_musica&artist=Metallica&came_from=67",
"description"=>"CBS RADIO's The Night Before with Metallica and Cage the Elephant",
"artists"=>
[{"name"=>"Metallica",
"mbid"=>"65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab",
"image_url"=>"https://s3.amazonaws.com/bit-photos/large/4304043.jpeg",
"thumb_url"=>"https://s3.amazonaws.com/bit-photos/thumb/4304043.jpeg",
"facebook_tour_dates_url"=>"http://www.bandsintown.com/Metallica/facebookapp?came_from=67",
"facebook_page_url"=>nil,
"tracker_count"=>2341049,
"url"=>"Metallica",
"website"=>"http://metallica.com"}],
"venue"=>{"name"=>"AT & T Park", "city"=>"San Francisco", "region"=>"CA", "country"=>"United States", "latitude"=>37.7784059, "longitude"=>-122.3894401}}]]
Found out why I am not getting anything while in production on Heroku, has to do with location=use_geoip on this line:
response = HTTParty.get("http://api.bandsintown.com/artists/#{URI.escape(follow.artist_name)}/events/search.json?api_version=2.0&app_id=#{app_id}&location=use_geoip")
This works locally because it can reference my location and then search Bandsintown for the correct information. However, on Heroku it does not know how to correctly reference a location. Therefore I don't receive any results.

How to map Amazon Products API to Rails?

I'm trying to fetch products from the Amazon Products API (using https://github.com/hakanensari/vacuum/) and display them in my Rails app. But how do I bring the product names and photos into my views?
Currently getting:
ActionView::Template::Error (undefined method `image' for #<Array:0x88486aec>):
2: <% if #products.any? %>
3: <% #products.each do |product| %>
4: <div class="product">
5: <%= link_to image_tag(product.image.url), product.url %>
6: <%= link_to product.name, product.url %>
7: </div>
8: <% end %>
main_controller.rb:
class MainController < ApplicationController
def index
request = Vacuum.new('GB')
request.configure(
aws_access_key_id: 'ABCDEFGHIJKLMNOPQRST',
aws_secret_access_key: '<long messy key>',
associate_tag: 'lipsum-20'
)
params = {
'SearchIndex' => 'Books',
'Keywords'=> 'Ruby on Rails'
}
#
# NOT SURE WHERE TO TAKE IT FROM HERE
#
raw_products = request.item_search(query: params)
#products = raw_products.to_h
product = OpenStruct.new(#products)
end
end
index.html.erb:
<% if #products.any? %>
<% #products.each do |product| %>
<div class="product">
<%= link_to image_tag(product.image.url), product.url %>
<%= link_to product.name, product.url %>
</div>
<% end %>
<% end %>
You are getting the error because raw_products isn't an array. AS par vacuum documentation, you will either have to convert it to hash by raw_products.to_h or You can also pass the response body into your own parser for some custom XML heavy-lifting:
MyParser.new(raw_products.body)
So you have to first parse the the response properly before consuming it.
You can just do following:
#products = raw_products.to_h
product = OpenStruct.new(#products)

RoR - ActionView Template Error

I'm trying to implement new redmine and then migrate my data from old redmine to new redmine.
But there is an template error showing in the log
ActionView::Template::Error (undefined method `is_closed?' for nil:NilClass):
6: <h3><%= format_activity_day(day) %></h3>
7: <dl>
8: <% sort_activity_events(#events_by_day[day]).each do |e, in_group| -%>
9: <dt class="<%= e.event_type %> <%= "grouped" if in_group %> <%= User.current.logged? && e.respond_to?(:event_author) && User.current == e.event_author ? 'me' : nil %>">
10: <%= avatar(e.event_author, :size => "24") if e.respond_to?(:event_author) %>
11: <span class="time"><%= format_time(e.event_datetime, false) %></span>
12: <%= content_tag('span', h(e.project), :class => 'project') if #project.nil? || #project != e.project %>
app/models/issue.rb:677:in `closed?'
app/models/issue.rb:57:in `block in <class:Issue>'
I guess this monday is occured when the css is rendered.
Is there any idea about this problem?
or how to log and find the real cause of the problem?
Thanks

Rails 3.1, can't make link_to image_path?

I want to make a fairly straightforward image link that uses an image from my assets. Getting weird errors. First I tried:
<%= link_to assets_path "town.png", 'index' %>
and got the error
Started GET "/" for 127.0.0.1 at Wed Nov 30 17:27:10 -0500 2011
Processing by PagesController#intro as HTML
Rendered pages/intro.html.erb within layouts/application (114.9ms)
Completed 500 Internal Server Error in 124ms
ActionView::Template::Error (undefined method `assets_path' for #<#<Class:0x10fdc4898>:0x10fdaad58>):
1: <body onload="init();">
2: <div id = "wrapper2">
3: <div class="intro_txt">
4: <%= link_to assets_path "town.png", 'index' %>
5: <br><br>
6: </div>
7: </div>
app/views/pages/intro.html.erb:4:in `_app_views_pages_intro_html_erb__1651075534_2280428740'
then I tried the old
<%= link_to image_tag "town.png", 'index' %>
and got this bizarre error
ActionView::Template::Error (undefined method `symbolize_keys!' for "index":String):
1: <body onload="init();">
2: <div id = "wrapper2">
3: <div class="intro_txt">
4: <%= link_to image_tag "townProjectText.png", 'index' %>
5: <br><br>
6: </div>
7: </div>
app/views/pages/intro.html.erb:4:in `_app_views_pages_intro_html_erb__1651075534_2279838600'
What to do?
<%= link_to image_tag('town.png'), 'index' %>
Put some parentheses
You need some ()
<%= link_to image_tag("town.png"), pages_path %>
Also, you need to use image_tag
<%= link_to image_tag("town.png"), image_path %>
if you want it to go to another page with the image by itself
Yeah some brackets would perhaps help. Try something like this:
<%= link_to(image_tag("town.png", :alt => 'Town Image'), index_url) %>

Resources