heroku undefined method empty? when upgrading my app to ruby 2.0 - ruby-on-rails

I've recently upgraded my Heroku app from Cedar-10 to Cedar-14 with no problems (still using ruby 1.9.3). Then I tried upgrading my app to use ruby 2.0.0-p645 and push it to the heroku server. When I do this I can no longer access my app and I get the following error in the logs;
2015-07-09T12:27:37.480991+00:00 app[web.1]:
2015-07-09T12:27:37.480996+00:00 app[web.1]: NoMethodError (undefined method `empty?' for nil:NilClass):
2015-07-09T12:27:37.480998+00:00 app[web.1]: app/controllers/wines_controller.rb:18:in `index'
2015-07-09T12:27:37.480999+00:00 app[web.1]:
2015-07-09T12:27:37.481001+00:00 app[web.1]:
2015-07-09T12:27:37.481462+00:00 app[web.1]: Processing by WinesController#index as HTML
2015-07-09T12:27:37.481465+00:00 app[web.1]: Completed 500 Internal Server Error in 100.1ms
If I look at line 18 of the wines_controller I have the following;
respond_to do |format|
format.html
format.json {render json: #wines.as_json}
end
I thought it was something in my index.html, so I took that back to the this;
%h1 Wines
With just this one line in my index.html.haml it still had a problem.
If I try and access wines.json this works and gives me a list of my wines. Running on my development box using 2.0.0 works fine and all my tests pass.
Update: Add controller & more info on index.html
Here is my wines controller
def index
# Search via Ransack
#q = current_user.wines.includes(:wine_rack).unconsumed.order("LOWER(winery)").search(params[:q])
#wines = #q.result.page params[:page]
#total = #q.result.sum(:qty)
respond_to do |format|
format.html
format.json {render json: #wines.as_json}
end
end
In regards to my index.html.haml file I uploaded a version of my project which only included this one line;
%h1 Wines
There are no loops happening in the view and I still get the error.
Line 18 refers to the following line in my controller;
respond_to do |format|

I found the answer. I had to upgrade my newrelic_rpm. It's the only thing I've changed and now it works.
- newrelic_rpm (3.5.0)
+ newrelic_rpm (3.12.1.298)
I tested by loading up another instance of heroku and sending my app there a few times with various bits of the controller & view cut out, nothing changed until I remarked out newrelic_rpm gem and it started working. So I upgraded it instead of removing the gem and now it works.
The only reason I removed it was due to me looking for differences between development and production. It was marked as a production only gem in my Gemfile.

Related

Rails NoMethodError for Blog#create on Heroku, but not Localhost

I have an app with a basic blog structure. Creating new blogs works perfectly on localhost, but when I try to create a new blog on Heroku I get the following error in my logs:
2018-07-11T21:20:01.863867+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] Command :: file -b --mime '/tmp/819c55b783715f61a2656207b4852b5c20180711-4-140ohfr.jpg'
2018-07-11T21:20:01.872443+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] Completed 500 Internal Server Error in 38ms (ActiveRecord: 0.0ms)
2018-07-11T21:20:01.873180+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0]
2018-07-11T21:20:01.873253+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] NoMethodError (undefined method `[]=' for nil:NilClass):
2018-07-11T21:20:01.873285+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0]
2018-07-11T21:20:01.873329+00:00 app[web.1]: [2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0] app/controllers/blogs_controller.rb:48:in `create'
2018-07-11T21:20:01.874027+00:00 app[web.1]: 10.101.219.132 - - [11/Jul/2018:21:19:56 UTC] "POST /blogs HTTP/1.1" 500 1958
2018-07-11T21:20:01.874063+00:00 app[web.1]: http://www.linchpinrealty.com/blogs/new -> /blogs
2018-07-11T21:20:01.874816+00:00 heroku[router]: at=info method=POST path="/blogs" host=www.linchpinrealty.com request_id=2f20a9d7-e0f6-4ab9-b2ac-d6b3a08e8ed0 fwd="68.225.227.137" dyno=web.1 connect=0ms service=5463ms status=500 bytes=2235 protocol=http
My blogs#create method is decently simple:
def create
#pillars = Pillar.all
#blog = Blog.new(blog_params)
if current_user.id = 1
#blog.user_id = 2
else
#blog.user = current_user
end
if #blog.save
redirect_to #blog, notice: 'Blog was successfully created.'
else
render :new
end
end
And I have the following permissions:
private
# Use callbacks to share common setup or constraints between actions.
def set_blog
#blog = Blog.friendly.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def blog_params
params.require(:blog).permit(:title, :teaser, :body, :user_id, :image, :tag_list, :link_text, :link_filename, :pillars_id)
end
I'm not sure where things are going off the rails (no pun intended). I did see this question where the issue was a database issue. In which case, the only recent change I've made would be in my blogs#show method...even though I have no idea how that would prevent a blog from even saving in the database (which it doesn't):
def show
#pillars = Pillar.all
#pillar = Pillar.find_by(id: #blog.pillars_id)
#related = Blog.where(pillars_id: #blog.pillars_id).where.not(id: #blog.id).limit(4)
#comment = #blog.comments.build
#comments = Comment.where(blog_id: #blog.id, approved: true)
if current_user
#user = current_user
end
end
Can anyone see where I'm going wrong?
From the logs and the fact it's working fine on your localhost, it's probably an error to host your tmp image file.
You should take a look at these articles:
Rails Active storage on Heroku
Rails Active storage exemple on Heroku
Edit:
I just saw you get the same error without an image, but don't you set any default image ?
-> Could you post error logs without uploaded image ?
Edit2:
I went on your website, I updated an image on an existing blog post and it works so it's probably not linked to the image system.
Edit3:
After some tests on your websites, it's the tag_list field which is wrong: you can create some new blog post without tags but as soon as you insert some tags an error is raised.
Ps: Sorry I did not managed to get my tests deleted without routes
Have you checked your migrations on Heroku?

Elasticsearch::Transport::Transport::Errors::BadRequest error heroku

I have a rails app which has search system which depends on elasticsearch, i have pushed it on heroku using the bonsai addon, but whenever i try to search something on my app it give me this error in the log.
2017-07-16T04:04:44.083489+00:00 app[web.1]: Completed 500 Internal Server Error in 18ms (ActiveRecord: 1.9ms)
2017-07-16T04:04:44.084229+00:00 app[web.1]: app/controllers/search_controller.rb:7:in `show'
2017-07-16T04:04:44.084222+00:00 app[web.1]: Elasticsearch::Transport::Transport::Errors::BadRequest ([400] {"error":{"root_cause":[{"type":"parsing_exception","reason":"no [query] registered for [filtered]","line":1,"col":22}],"type":"parsing_exception","reason":"no [query] registered for [filtered]","line":1,"col":22},"status":400}):
My Elasticsearch Controller
class SearchController < ApplicationController
before_action :beautify_url
layout "simple"
def show
#post_records = Post.search(query_term).paginate(page: params[:page]).records
#posts = #post_records.to_a.select { |post| post.published? }
#users = User.search(query_term).records.to_a
#tags = Tag.search(query_term).records
end
def users
#users = User.search(query_term).records.to_a
end
private
def beautify_url
if params[:search].present?
case params[:action]
when "show"
redirect_to search_url(q: params[:search][:q])
when "users"
redirect_to search_users_url(q: params[:search][:q])
end
end
end
def query_term
params[:q] || ''
end
end
Please help!!
Bonsai support here. Bonsai clusters are currently being provisioned on Elasticsearch 5.x, and as of Elasticsearch 5.0, the filtered query has been removed. Attempting to use a filtered query in 5.x results in that error message you're seeing.
From what you've shared, I would say the most likely issue is that the client is using a deprecated version of the Query DSL. That would suggest an incompatible gem version.
You can check what version your Elasticsearch gems are by running this from a command line:
bundle show | grep elasticsearch
If they are not 5.x.x, update them in your Gemfile:
gem "elasticsearch", "~> 5"
gem "elasticsearch-rails", "~> 5"
And run bundle update elasticsearch elasticsearch-rails. Push the change to Heroku and try your search again.
If that doesn't help, shoot an email to support#bonsai.io and we'll help you sort it out.

Ruby error with mailgun on heroku server

I'm trying to make a 'contact us' page on a heroku server using mailgun. I am fairly confident I set it up right (I'm being spoon fed this project by following upskillcources.com), but I keep getting this error anyways "We're sorry, but something went wrong."
Here is the heroku logs that seem applicable to me:
2017-05-05T06:16:10.020320+00:00 app[web.1]: WHERE a.attrelid = '"contacts"'::regclass
2017-05-05T06:16:10.020321+00:00 app[web.1]: AND a.attnum > 0 AND NOT a.attisdropped
2017-05-05T06:16:10.020322+00:00 app[web.1]: ORDER BY a.attnum
2017-05-05T06:16:10.020323+00:00 app[web.1]: ):
2017-05-05T06:16:10.020344+00:00 app[web.1]: F, [2017-05-05T06:16:10.020309 #4] FATAL -- : [2777b122-a496-4a3d-b6b7-08b32fc56cd4]
2017-05-05T06:16:10.020377+00:00 app[web.1]: F, [2017-05-05T06:16:10.020344 #4] FATAL -- : [2777b122-a496-4a3d-b6b7-08b32fc56cd4] app/controllers/contacts_controller.rb:3:in `new'
I have no idea what could be wrong with the code in the error logs, especially because I can see that the contacts_controller.rb file is identical to identical project codes found on the github's of multiple people using the same resource upskill resource.
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
name = params[:contact][:name]
email = params[:contact][:email]
body = params[:contact][:comments]
ContactMailer.contact_email(name, email, body).deliver
flash[:success] = "Message sent."
redirect_to new_contant_path #this should be contact path I think, but a c9 error suggested this instead and wouldn't work without the change dispite it being different than the codes on github for the same project.
else
flash[:danger] = #contact.errors.full_messages.join(", ")
redirect_to new_contact_path
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
Please also let me know for future ref how I can be better/more specific at asking questions here, so sorry for how difficult I am making it, this is my first programming project beyond 'hello world' and a to do app, thanks so much for taking the time!
Your logs says that you don't have a table in database.
You need to create and run migration. Details here: http://edgeguides.rubyonrails.org/active_record_migrations.html
Also you need to remember that Heroku doesn't run migrations for your by default. So you will need to call heroku run rake db:migrate after deploying your code. But you can automate this process by adding release command to your Procfile. Detailed instruction here: http://aspiringwebdev.com/run-rails-migrations-automatically-on-heroku/

Why am I getting NoMethodError with my rails app on AppFog?

I have a rails app on AppFog, connected to a mongo database. It is a very simple database, where I used the mongo_mapper gem to create MVC stuff, and then didn't modify it.
rails g scaffold Contacts name:string address:string email:string phone:string -orm mongo_mapper
REALLY basic! Just trying to test out mongo as a database.
I followed the instructions for talking with the database here.
However, every time I try to access the /contacts path, I get a "500 Internal Server Error" message.
The AppFog logs for my app report:
Started GET "/contacts" for 50.193.89.38 at 2013-04-03 21:18:58 +0000
Processing by ContactsController#index as HTML
Completed 500 Internal Server Error in 0ms
NoMethodError (undefined method `collection' for nil:NilClass):
app/controllers/contacts_controller.rb:5:in `index'
The first chunk of my contacts controller file:
class ContactsController < ApplicationController
# GET /contacts
# GET /contacts.json
def index
#contacts = Contact.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #contacts }
end
end
....
Any help would be great.
==UPDATE==
$ rails console
irb(main):001:0> Contact.all
=> []
And using rails s works fine, I don't get the 500 error at all.
The name of the model should be singular, ie Contactand not Contacts.
rails g scaffold Contact name:string address:string email:string phone:string -orm mongo_mapper
The way you created, makes the name of the model be Contacts and not Contact. So when you try Contact.all you get nil.

undefined method `parent' for nil:NilClass

I'm getting this strange error using Rails 3.0.2.
ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'
This is the controller, and line 19 is the respond_with(#channels) block.
Where do I start to search for errors?
class ChannelsController < ApplicationController
before_filter :set_default_client
respond_to :html, :xml
def index
if params[:cache_set]
#channels = Channel.active.find_all_by_id(params[:cache_set])
else
#channels = Channel.active.find_all_by_id(cookies[:channels].split(','))
end
respond_with(#channels)
end
end
This is the full error:
ActionView::Template::Error (undefined method `parent' for nil:NilClass):
app/controllers/channels_controller.rb:19:in `index'
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (19.8ms)
Rendered /Users/linus/.rvm/gems/ruby-1.8.7-p330/gems/actionpack-3.0.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (28.6ms)
I'm using Ruby 1.8.7 with Rails 3.0.2.
I've also, just in case, tried Rails 3.0.7 and 3.0.0.
Are you in any case using HAML?
I just bumped into this as well and my colleague (who is not on Stackoverflow yet) found out that it was due to multiline comments in the view template
-#
= helper_method_1
= helper_method_2
I solved the problem by changing HAML version from 3.1.x to 3.0.24.
My new Gemfile looks like this.
gem "rails", "3.0.2"
gem "haml", "3.0.24"
gem "compass", "0.10.6"
It looks like HAML-edge has fixed this problem.
I expect the next release of HAML after 3.1.1 to resolve this.

Resources