Heroku can't find user but works in dev - ruby-on-rails

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

Related

uninitialized constant Formtastic::I18n::SCOPES

Rails 6.0.1, Puma 4.3.0, Devise 4.7.1, ActiveAdmin 2.4.0, Formtastic 3.1.5.
Once again I am creating a Rails application. Created the foundation. A couple of models. Made a simple front-end for them. Next install Devise, ActiveAdmin.
In general, nothing unusual. Locally everything works fine.
But on the server, some kind of nonsense began to happen with ActiveAdmin.
In general, the application on the server is working fine. Through the console, I can create all the data. These data are successfully displayed on the site.
But if I go into ActiveAdmin...
Dashboard page displayed successfully. But if I go to the index page of any entity, then I will get the following error (from the log):
2019-11-19T00:55:04.216309411Z app[web.1]: web| I, [2019-11-19T00:55:04.198819 #14] INFO -- : [9ac98910-3a38-496d-9f5e-f276bc590ba7] Rendered vendor/bundle/ruby/2.5.0/gems/activeadmin-2.4.0/app/views/active_admin/resource/index.html.arb (Duration: 73.2ms | Allocations: 17729)
2019-11-19T00:55:04.216369240Z app[web.1]: web| I, [2019-11-19T00:55:04.199305 #14] INFO -- : [9ac98910-3a38-496d-9f5e-f276bc590ba7] Completed 500 Internal Server Error in 86ms (ActiveRecord: 5.8ms | Allocations: 19675)
2019-11-19T00:55:04.216376506Z app[web.1]: web| F, [2019-11-19T00:55:04.203841 #14] FATAL -- : [9ac98910-3a38-496d-9f5e-f276bc590ba7]
2019-11-19T00:55:04.216380926Z app[web.1]: web| [9ac98910-3a38-496d-9f5e-f276bc590ba7] ActionView::Template::Error (uninitialized constant Formtastic::I18n::SCOPES
2019-11-19T00:55:04.216385058Z app[web.1]: web| Did you mean? Sprockets):
2019-11-19T00:55:04.216388027Z app[web.1]: web| [9ac98910-3a38-496d-9f5e-f276bc590ba7] 1: insert_tag renderer_for(:index)
The entire log: https://pastebin.com/raw/buWSveBZ
Only one section for entities works - this is the show action. Only two actions for entities work - these are show and destroy. Everyone else catches the error that I showed above.
I absolutely don't understand what's the matter.
I successfully use the identical config/initializers/active_admin.rb file in two other Rails 6 applications. Below I will show an example of one of the file for ActiveAdmin:
# frozen_string_literal: true
ActiveAdmin.register User do
menu priority: 5
permit_params :email, :full_name, :roles, :password, :password_confirmation
remove_filter :users_roles
controller do
def find_resource
scoped_collection.find_by!(pkey: params[:id])
end
end
index do
selectable_column
id_column
column :pkey
column :email
column :full_name
column :roles
column :current_sign_in_at
column :sign_in_count
column :created_at
actions
end
form do |f|
f.inputs do
f.input :email
f.input :full_name
f.input :roles
f.input :password
f.input :password_confirmation
end
f.actions
end
end
I'm hope for your help.
For what it's worth, I have a rails 6 app that I hadn't touched in about a year and was getting a similar error despite never having an issue with my other rails app that I use regularly -- uninitialized constant Formtastic::Util
Commenter above's advice of running rails generate formtastic:install did the trick for me. Thanks!

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?

ActionView::Template::Error (undefined method `force_encoding' for nil:NilClass):

I'm using Gems in 'vendor/bundler'. When I got Encoding error, I fixed it by adding a file named 'output_safty_encoding_patch.rb' in 'config/initializer':
output_safty_encoding_patch.rb
module ActiveSupport
class SafeBuffer < String
def concat(value)
if value.html_safe?
super(value.force_encoding('utf-8'))
else
super(ERB::Util.h(value.force_encoding('utf-8')))
end
end
alias << concat
end
end
Then I used gem 'devise' and tried to create a new post.
I got this error.
ActionView::Template::Error (undefined method `force_encoding' for nil:NilClass):
My logs:
Processing by PostsController#new as HTML
Rendered posts/_form.html.slim (30.2ms)
Rendered posts/new.html.erb within layouts/application (36.8ms)
Completed 500 Internal Server Error in 43ms
ActionView::Template::Error (undefined method `force_encoding' for nil:NilClass):
1: = form_for(#post) do |f|
2: -if #post.errors.any?
3: #error_explanation
4: h2= pluralize(#post.errors.count, "error")
config/initializers/output_safty_encoding_patch.rb:7:in `concat'
app/views/posts/_form.html.slim:1:in _app_views_posts__form_html_slim__2419859499811208315_70341230451880
app/views/posts/new.html.erb:4:in _app_views_posts_new_html_erb___3935161508588949089_70341230567660
You may want to reconsider the monkey-patch of ActiveSupport::Safebuffer (the file you've got in config/initializers/output_safty_encoding_patch.rb).
Altering behavior of a core class is bound to run into issues that are difficult to debug (like this one).
Maybe you could post a question about the problem that led you to the monkey-patch in the first place?

unicorn fails when started by capistrano

When I start unicorn via capistrano task and try to access my app I get this
tarted GET "/" for xx.xxx.xxx at 2012-08-11 01:38:31 +0000
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (0.1ms)
Completed 500 Internal Server Error in 87ms
** [Bugsnag] No API key configured, couldn't notify
ActionView::Template::Error (undefined method `split' for nil:NilClass):
32: </form>
33: </ul>
34: <ul class="nav pull-right">
35: <% if user_signed_in? %>
36: <li class="dropdown">
37: <a href="#" class="dropdown-toggle" data-toggle="dropdown">
38: <%= current_user.name %>
app/controllers/application_controller.rb:5:in `current_user'
app/controllers/application_controller.rb:9:in `user_signed_in?'
app/views/layouts/application.html.erb:35:in `_app_views_layouts_application_html_erb__3931886679734892787_26070680'
in addition of ActionView::Template::Error, unicorn says that no busgnag API key configured, but I've already set the key on my root's .bashrc and deploy's .bashrc (deploy is a user too)
but when I access the server via ssh like ssh root#server and try to start unicorn with the same command the capistrano's task issued, everythin works fine...
I've tried to set root user on capistrano and deploy user, both with use_sudo true and false...
What am I missing here?
Thanks
Are you sure when you run it manually you are running in production mode? undefined methodxxxx' for nil:NilClass` happens when you have an nil variable. Often times code like...
Articles.each do |article|
article.title
end
Will fails with the same error because the db is populated differently in each environment. I would suggest looking at line 5 application_controller.rb and seeing if that variable is present in both production and development.
Also, make sure that your bugsnap api key is available in your production.rb file.

Rails 3 Heroku app, FileUtils error server side

I am attempting to code a very simple way for a user to add html files to my Heroku app. These files would be saved in ./log for rendering later. I have tested my code locally (in both development and production), but when I attempt to upload a file on my Heroku hosted repo I get internal server error 500.
controller upload.rb:
class UploadController < ApplicationController
def index
render :file => 'upload/uploadfile.haml'
end
def uploadFile
file_param = params[:upload][:datafile]
post = DataFile.save(file_param)
render :text => "File has been uploaded successfully"
end
end
model data_file.rb:
class DataFile < ActiveRecord::Base
def self.save(upload)
# Changed Default Destination: [__RAILS_DIR__/log]
name = "./log/" + upload.original_filename
# can haz data directory?
require 'FileUtils'
FileUtils.mkdir_p(File.dirname(name))
File.open(name, "wb") { |f| f.write(upload.read) }
end
end
view uploadfile.haml:
%h1 File Upload
= form_for :upload,:url=>{:action => 'uploadFile'},:html => { :multipart => true } do |f|
%p
%label{:for => "upload_file"} Select File
\:
\#{f.file_field 'datafile'}
= f.submit "Upload"
heroku logs:
2012-08-07T14:13:20+00:00 app[web.1]: Started POST "/uploadFile" for 69.29.117.99 at 2012-08-07 14:13:20 +0000
2012-08-07T14:13:20+00:00 app[web.1]: Processing by UploadController#uploadFile as HTML
2012-08-07T14:13:20+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "authenticity_token"=>"1dAXkMulNR0d8S/l6QC8JnpSDtNBaHoyKJezgnheR10=", "upload"=>{"datafile"=>#>}, "commit"=>"Upload"}
2012-08-07T14:13:20+00:00 app[web.1]: Completed 500 Internal Server Error in 3ms
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]: LoadError (no such file to load -- FileUtils):
2012-08-07T14:13:20+00:00 app[web.1]: app/models/data_file.rb:7:in save'
2012-08-07T14:13:20+00:00 app[web.1]: app/controllers/upload_controller.rb:8:inuploadFile'
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]:
2012-08-07T14:13:20+00:00 app[web.1]: cache: [POST /uploadFile] invalidate, pass
heroku: http://upload-example.herokuapp.com/
github: https://github.com/halterj1/upload
Please no trying to convince me to use paperclip or carrierwave, that does not answer my question. Any help would be greatly appreciated, thanks in advance guys!
You should read this article on heroku: https://devcenter.heroku.com/articles/read-only-filesystem
edit:
As stated in article.
Your app is compiled into a slug for fast distribution across the dyno manifold. The filesystem for the slug is read-only, which means you cannot dynamically write to the filesystem for semi-permanent storage. The following types of behaviors are not supported:
Caching pages in the public directory
Saving uploaded assets to local disk (e.g. with attachment_fu or paperclip)
Writing full-text indexes with Ferret
Writing to a filesystem database like SQLite or GDBM
Accessing a git repo for an app like git-wiki
There are two directories that are writeable: ./tmp and ./log (under your application root). If you wish to drop a file temporarily for the duration of the request, you can write to a filename like #{RAILS_ROOT}/tmp/myfile_#{Process.pid}. There is no guarantee that this file will be there on subsequent requests (although it might be), so this should not be used for any kind of permanent storage.

Resources