Ruby error with mailgun on heroku server - ruby-on-rails

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/

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?

Facebook login by omniauth on heroku is not working

I built an app with Facebook login function by Rails which perfectly worked on localhost, but now it doesn't work on Heroku. It looks like a common problem everyone gets, but none of the past questions or other articles helped.
error image
The above link goes to the error image. It should be coming from Heroku but Facebook because I saw the same error when I was dealing with Stripe. Before this error started bothering me, there was another error from Facebook saying Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings. but it was solved by adding the Heroku url to the Facebook app page.
I did figaro heroku:set -e production so the app keys and secrets mush have been set in Heroku.
Here are some codes from my files;
config/initializers/devise.rb
config.omniauth :facebook, ENV["facebook_app_id"], ENV["facebook_app_secret"], scope: 'email', info_fields: 'email,name', secure_image_url: true
app/models/user.rb
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.image = "http://graph.facebook.com/#{auth.uid}/picture?type=large" # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
controllers/users/omniauth_callback_controller.rb
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def failure
redirect_to root_path
end
end
heroku logs
2017-07-17T15:33:54.234171+00:00 app[web.1]: Started GET "/users/auth/facebook/callback?code=AQCoKbzr4 ///// 00703" for 150.116.22.144 at 2017-07-17 15:33:54 +0000
2017-07-17T15:33:54.236011+00:00 app[web.1]: I, [2017-07-17T15:33:54.235951 #4] INFO -- omniauth: (facebook) Callback phase initiated.
2017-07-17T15:33:54.360053+00:00 app[web.1]: Processing by Users::OmniauthCallbacksController#facebook as HTML
2017-07-17T15:33:54.360097+00:00 app[web.1]: Parameters: {"code"=>"AQCoKbzr4nv6c7BEpM ///// 86c27a00703"}
2017-07-17T15:33:54.371557+00:00 app[web.1]: User Load (1.8ms) SELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 ORDER BY "users"."id" ASC LIMIT 1 [["provider", "facebook"], ["uid", "102081518247"]]
2017-07-17T15:33:54.581790+00:00 heroku[router]: at=info method=GET path="/users/auth/facebook/callback?code=AQCoK ///// a00703" host=xxxxxxx-xxxx-xxxxx.herokuapp.com request_id=93945-1199-417e-8d98-ede264cb fwd="150.116.22.144" dyno=web.1 connect=1ms service=350ms status=500 bytes=1754 protocol=https
2017-07-17T15:33:54.578410+00:00 app[web.1]: Completed 500 Internal Server Error in 218ms (ActiveRecord: 3.0ms)
2017-07-17T15:33:54.579175+00:00 app[web.1]:
2017-07-17T15:33:54.579178+00:00 app[web.1]: RuntimeError (redirection forbidden: http://graph.facebook.com/102087018247/picture?type=large -> https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13064_10202475740292_410664266178542_n.jpg?oh=ef118e9d947604c9c7055a92e2&oe=5A02F8B4):
2017-07-17T15:33:54.579178+00:00 app[web.1]: app/models/user.rb:18:in `block in from_omniauth'
2017-07-17T15:33:54.579179+00:00 app[web.1]: app/models/user.rb:14:in `from_omniauth'
2017-07-17T15:33:54.579180+00:00 app[web.1]: app/controllers/users/omniauth_callbacks_controller.rb:4:in `facebook'
2017-07-17T15:33:54.579180+00:00 app[web.1]:
2017-07-17T15:33:54.579181+00:00 app[web.1]:
I have no idea what RuntimeError from the Heroku logs indicates... Any clue or advice would be appreciated.
You got redirection error because the image url will redirect user to another url. and there is a limitation in the open-uri when redirect http to https.
In the error message you can see this url: http://graph.facebook.com/102087018247/picture?type=large will be redirected to https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/13064_10202475740292_410664266178542_n.jpg?oh=ef118e9d947604c9c7055a92e2&oe=5A02F8B4
you can work around this issue by replacing http with https in your image url
"https://graph.facebook.com/#{auth.uid}/picture?type=large"
or using this way:
user.remote_image_url = auth.info.image.gsub(/\Ahttp:/, "https")
mMake sure you have whitelisted the production applications domain in Facebook Developers Console.
I usually set up a sub test app from my default app, test app has its own keys and set up ENV for them and localhost is whitelisted. This way development is easier
Then have ENV set for production app inside your App and Heroku, with Heroku domain whitelisted. Make sure your callback contains the Heroku production domain, matching the one you whitelisted
Then migrating Heroku database after the push to Heroku (this one works often for me)
heroku run rake db:migrate
Btw the way your accessing the image is different that how i've done it.
user.remote_avatar_url = auth.info.image
if this doesn't work, tell me, i've set up a few Facebook login on Heroku in my time.

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.

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

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.

heroku error - NoMethodError (undefined method `updated?'

I've a rails 3.1 app on Heroku, I keep getting an 500 error in production which I can't recreate in dev.
When I try to perform an update action in one of my controllers I get a 500. I get the following from heroku logs -
2011-12-05T13:52:35+00:00 app[web.1]: Completed 500 Internal Server Error in 15ms
2011-12-05T13:52:35+00:00 app[web.1]:
2011-12-05T13:52:35+00:00 app[web.1]: NoMethodError (undefined method `updated?' for #<ActiveRecord::Associations::HasOneAssociation:0x00000004058800>):
2011-12-05T13:52:35+00:00 app[web.1]: app/controllers/cars_controller.rb:81:in `block in update'
2011-12-05T13:52:35+00:00 app[web.1]: app/controllers/cars_controller.rb:80:in `update'
The update action on my cars controller is -
def update
#car = Car.find_by_url_identifier(params[:id])
respond_to do |format| #**line 80**
if #car.update_attributes(params[:car]) #**line 81**
CarMailer.gift_confirmation(#car).deliver
format.html { redirect_to(thanks_path(#car.url_identifier)) }
else
format.html { render action: "edit" }
end
end
end
The parameters being posted are -
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"RTXCVvdsKQHc8CxHLeYS9WuztcrI1b4H8SHsdfsKWu+Iz4=",
"car"=>{"name"=>"dgdfsdfsd",
"email"=>"test#test.com",
"recipient_attributes"=>{"name"=>"fdsfsd",
"address1"=>"sdfsdfdsf",
"address2"=>"dsfdsfsdf",
"city"=>"sdfdsf",
"postcode"=>"sdfds"},
"gift_id"=>"2",
"message"=>"fsdfsdfdsf",
"terms"=>"1"},
"commit"=>"submit",
"id"=>"test5"}
My car model looks like this -
class Car < ActiveRecord::Base
validates :name, :presence =>true, :on => :update
validates :url_identifier, :presence =>true, :on => :create
has_one :recipient
belongs_to :gift
accepts_nested_attributes_for :recipient
accepts_nested_attributes_for :gift
def to_param
self.url_identifier
end
end
Any idea how I can fix this? Apart from looking at herokus logs, how else can I debug this?
Oddly the app work for a while if I do a 'heroku restart'
I can't see anything wrong in the code. Since it works on your development machine, I am guessing it is something else. Some difference. Did you run your migrations on heroku?
After deploying on heroku, you have to run your migrations in a separate step. I am not sure if that would give this kind of error. Just guessing here.
I googled the error message. Have you had a look at this thread: http://ruby-forum.com/topic/81719 They had a similar problem and have posted their solution/hack. Might be worth a try.
It looks like you are trying to call the method update to an ActiveRecord::Associations object instead of (probably) the Car model, which is strange because you would think it would be fetched and called on the model if it isn't a method found in Arel.
You could try to debug this by using the production environment settings in your development environment by looking in config/enviroments/production.rb and using those settings for development.
You could also try and recreate the problem on heroku using heroku console

Resources