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

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?

Related

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.

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/

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 can't find user but works in dev

I'm hitting a problem with my heroku deployment. Here is the log:
2014-04-29T16:41:59.782999+00:00 app[web.1]: Started GET "/lines/33/edit" for 94.174.113.168 at 2014-04-29 16:41:59 +0000
2014-04-29T16:41:59.782933+00:00 app[web.1]: Started GET "/lines/33/edit" for 94.174.113.168 at 2014-04-29 16:41:59 +0000
2014-04-29T16:41:59.804819+00:00 app[web.1]:
2014-04-29T16:41:59.804823+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=0):
2014-04-29T16:41:59.804826+00:00 app[web.1]: app/views/lines/_history.html.erb:7:in `block in _app_views_lines__history_html_erb__3033746240219592358_69949428301200'
2014-04-29T16:41:59.804824+00:00 app[web.1]: config/initializers/paper_trail.rb:4:in `user'
2014-04-29T16:41:59.804854+00:00 app[web.1]: app/views/lines/_history.html.erb:4:in `_app_views_lines__history_html_erb__3033746240219592358_69949428301200'
2014-04-29T16:41:59.804828+00:00 app[web.1]: app/views/lines/_history.html.erb:4:in `each'
2014-04-29T16:41:59.804856+00:00 app[web.1]: app/views/lines/edit.html.erb:16:in `_app_views_lines_edit_html_erb__4129892984524241027_69949427894700'
2014-04-29T16:41:59.804869+00:00 app[web.1]: app/views/lines/_history.html.erb:4:in `each'
2014-04-29T16:41:59.804871+00:00 app[web.1]: app/views/lines/_history.html.erb:4:in `_app_views_lines__history_html_erb__3033746240219592358_69949428301200'
2014-04-29T16:41:59.804872+00:00 app[web.1]: app/views/lines/edit.html.erb:16:in `_app_views_lines_edit_html_erb__4129892984524241027_69949427894700'
2014-04-29T16:41:59.804875+00:00 app[web.1]:
2014-04-29T16:41:59.804874+00:00 app[web.1]:
2014-04-29T16:41:59.804857+00:00 app[web.1]:
2014-04-29T16:41:59.804858+00:00 app[web.1]:
2014-04-29T16:41:59.804864+00:00 app[web.1]:
2014-04-29T16:41:59.804865+00:00 app[web.1]: ActiveRecord::RecordNotFound (Couldn't find User with id=0):
2014-04-29T16:41:59.804866+00:00 app[web.1]: config/initializers/paper_trail.rb:4:in `user'
2014-04-29T16:41:59.804868+00:00 app[web.1]: app/views/lines/_history.html.erb:7:in `block in _app_views_lines__history_html_erb__3033746240219592358_69949428301200'
The offending initializer is as follows:
module PaperTrail
class Version < ActiveRecord::Base
def user
User.find self.whodunnit.to_i
end
def nextversion
self.next
end
end
end
Which is getting called in the _history view with the following:
<%= gravatar_for version.user %><%= link_to version.user.username, user_path(version.user) %>
This works fine in development and appears to work fine in production on most edit pages but on one I am encountering this error. I wanted to check the version in the console but it seems I can't access paper_trail Versions in the console at all, I can only assume this is because I don't have a Version model, only a table and a controller.
How can I fix this?
You are getting following error
ActiveRecord::RecordNotFound (Couldn't find User with id=0)
on the following method
def user
puts "Value of whodunnit is -----------> #{self.whodunnit}"
User.find self.whodunnit.to_i
end
which is called in
<%= gravatar_for version.user %><%= link_to version.user.username, user_path(version.user) %>
because self.whodunnit.to_i is returning 0. That means, whodunnit is either "" or nil or 0. Please set the correct value of whodunnit attribute. Also, you can check the current value of whodunnit by adding a logger or puts statement in PaperTrail::Version# user method.
You probably did not ran your seed.rb file (heroku run rake db:seed) to create the user or you just are looking for an user that is not defined anymore (like deleted for example).
When you delete a user that had id=0 the next user will have id=1.
Just for sure, o recommend that you enter on your console (heroku run console) and try to find it by yourself (Client.find(0)) or try to list all of them (Client.all) to see if the user with id=0 is defined.
Hope it helps

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