Form works in development but not production - Heroku - ruby-on-rails

I have a voting platform that I'm building for my company and I am very close to finishing it but the actual voting is failing while in production. I've done all my migrating and everything, and it's still working in development.
Here are the logs I'm receiving from heroku:
2019-01-11T18:33:59.235681+00:00 app[web.1]: I,
[2019-01-11T18:33:59.235579 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Started POST
"/ballots/13/votes" for 75.150.214.2 at 2019-01-11 18:33:59 +0000
2019-01-11T18:33:59.236373+00:00 app[web.1]: I,
[2019-01-11T18:33:59.236296 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Processing by
VotesController#create as HTML 2019-01-11T18:33:59.236437+00:00
app[web.1]: I, [2019-01-11T18:33:59.236378 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Parameters: {"utf8"=>"✓",
"authenticity_token"=>"Y/vAh2kYPskozS2yQn3Ky1fOq41abLUjlxho0Q+zXHSndVlaSfwYz7bdoqvintdBXrYNDhsbIduYICpUAx3/0g==",
"user_vote"=>"Why", "vote"=>{"comment"=>""}, "commit"=>"Cast Vote",
"ballot_id"=>"13"} 2019-01-11T18:33:59.239540+00:00 app[web.1]: D,
[2019-01-11T18:33:59.239464 #4] DEBUG -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Ballot Load (0.7ms) SELECT
"ballots".* FROM "ballots" WHERE "ballots"."id" = $1 LIMIT $2 [["id",
13], ["LIMIT", 1]] 2019-01-11T18:33:59.240333+00:00 app[web.1]: I,
[2019-01-11T18:33:59.240244 #4] INFO -- :
[772bc37c-00ee-43da-aa81-564d353553bb] Completed 500 Internal Server
Error in 4ms (ActiveRecord: 0.7ms) 2019-01-11T18:33:59.240846+00:00
app[web.1]: F, [2019-01-11T18:33:59.240770 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
2019-01-11T18:33:59.240917+00:00 app[web.1]: F,
[2019-01-11T18:33:59.240847 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
ActiveModel::UnknownAttributeError (unknown attribute 'comment' for
Vote.):' '2019-01-11T18:33:59.240981+00:00 app[web.1]: F,
[2019-01-11T18:33:59.240914 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
2019-01-11T18:33:59.241051+00:00 app[web.1]: F,
[2019-01-11T18:33:59.240987 #4] FATAL -- :
[772bc37c-00ee-43da-aa81-564d353553bb]
app/controllers/votes_controller.rb:10:in `create'
2019-01-11T18:34:07.533587+00:00 heroku[run.2070]: State changed from
up to complete bobby#bobby-Al
Here's my code for vote create
def create
#ballot = Ballot.find(params[:ballot_id])
#vote = #ballot.votes.new(user_vote: params[:user_vote], comment: params[:vote][:comment])
#vote.user_id = current_user.id
if !current_user.votes.find_by(ballot_id: params[:ballot_id])
#vote.save
redirect_to ballot_path(params[:ballot_id])
else
flash[:alert] = "You've already voted on this ballot"
redirect_to ballot_path(params[:ballot_id])
end
end

It seems like a migration problem, just make sure to run your migrations for the environment production on heroku like this:
heroku run rake db:migrate
Restart your app
heroku restart --app my_app_name
Important
If you generate a migration that creates a table and make a push to heroku, to add another column to that table you shouldn't modify that one, instead you should create another one for that.
When you run rake db:migrate, rails register that on the table schema_migrations, so if you modify a migrate that is already registered is not going to be executed again. This could be the case, check directly on your database and see if the column is there.

Related

Rails App works locally, but not in production with Heroku

I've searched through the previous questions and there were a couple that were similar to mine - but the solutions for them didn't work for me. Myapp works fine locally, but not on Heroku when I attempt to create comments (as a User, which I'm using devise for). I'll try to give as much detail as I can.
I get the following error:
We're sorry, but something went wrong.
If you are the application owner check the logs for more information.
Then i do
heroku logs --tail
and I see this
2021-05-26T14:34:26.408158+00:00 app[web.1]: I, [2021-05-26T14:34:26.408029 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Started POST "/projects/1/comments" for 103.85.38.191 at 2021-05-26 14:34:26 +0000
2021-05-26T14:34:26.409593+00:00 app[web.1]: I, [2021-05-26T14:34:26.409489 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Processing by CommentsController#create as HTML
2021-05-26T14:34:26.413951+00:00 app[web.1]: I, [2021-05-26T14:34:26.413812 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Parameters: {"authenticity_token"=>"[FILTERED]", "comment"=>{"body"=>"hey"}, "commit"=>"Create Comment", "project_id"=>"1"}
2021-05-26T14:34:26.435342+00:00 app[web.1]: I, [2021-05-26T14:34:26.435230 #4] INFO -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff] Completed 500 Internal Server Error in 21ms (ActiveRecord: 4.0ms | Allocations: 1086)
2021-05-26T14:34:26.436572+00:00 app[web.1]: F, [2021-05-26T14:34:26.436494 #4] FATAL -- : [0aa9a88d-47f4-42b9-a93a-658d40d61fff]
2021-05-26T14:34:26.436575+00:00 app[web.1]: [0aa9a88d-47f4-42b9-a93a-658d40d61fff] ActiveModel::MissingAttributeError (can't write unknown attribute `user_id`):
2021-05-26T14:34:26.436576+00:00 app[web.1]: [0aa9a88d-47f4-42b9-a93a-658d40d61fff]
2021-05-26T14:34:26.436576+00:00 app[web.1]: [0aa9a88d-47f4-42b9-a93a-658d40d61fff] app/controllers/comments_controller.rb:5:in `create'
So I tried what the other questions recommended for similar issues, I did
heroku rails/rake db migrate
heroku restart
heroku pg:reset DATABASE
None of them worked, or had any effect on the error.
For reference, here is the block of code (in CommentsController) that the error is referencing.
class CommentsController < ApplicationController
def create
#project = Project.find(params[:project_id])
#comment = #project.comments.create(comment_params)
#comment.user = current_user
#comment.save
redirect_to project_path(#project)
end
private
def comment_params
params.require(:comment).permit(:user_id, :body)
end
end
Your Comment model probably has belongs_to set like this
belongs_to :user, class_name: 'User'
but your comment table in the database doesn't have a user_id set. Maybe you inserted the column manually on your local database. If so, create a migration adding the column.
If you want to check how your comments table is on heroku, open rails console on heroku with heroku run rails c and run
ActiveRecord::Base.connection.columns('comments').map(&:name) # change 'comments' if your table has a different name
It will return your comments table columns

Getting an error in postgresql where column name cannot be found?

I am getting the following error in my Heroku logs when a query in my rails application is being invoked :
2019-03-19T02:16:25.782434+00:00 app[web.1]: I, [2019-03-19T02:16:25.782337 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Started GET "/patients?utf8=
%E2%9C%93&Full_Name=tester" for 95.45.117.131 at 2019-03-19 02:16:25 +0000
2019-03-19T02:16:25.783395+00:00 app[web.1]: I, [2019-03-19T02:16:25.783337 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Processing by PatientsContro
ller#index as HTML
2019-03-19T02:16:25.783570+00:00 app[web.1]: I, [2019-03-19T02:16:25.783521 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Parameters: {"utf8"=>"✓",
"Full_Name"=>"tester"}
2019-03-19T02:16:25.790532+00:00 app[web.1]: D, [2019-03-19T02:16:25.790467 #4] DEBUG -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] User Load (3.7ms) SELECT
"users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 5], ["LIMIT", 1]]
2019-03-19T02:16:25.794827+00:00 app[web.1]: D, [2019-03-19T02:16:25.794753 #4] DEBUG -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] (3.2ms) SELECT COUNT(*)
FROM "patients" WHERE (full_name LIKE '%tester%' AND user_id = 5)
2019-03-19T02:16:25.795077+00:00 app[web.1]: I, [2019-03-19T02:16:25.795025 #4] INFO -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] Completed 500 Internal Serve
r Error in 11ms (ActiveRecord: 6.9ms)
2019-03-19T02:16:25.795954+00:00 app[web.1]: F, [2019-03-19T02:16:25.795899 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec]
2019-03-19T02:16:25.796028+00:00 app[web.1]: F, [2019-03-19T02:16:25.795980 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] ActiveRecord::StatementInval
id (PG::UndefinedColumn: ERROR: column "full_name" does not exist
2019-03-19T02:16:25.796030+00:00 app[web.1]: LINE 1: SELECT COUNT(*) FROM "patients" WHERE (full_name LIKE '%test...
2019-03-19T02:16:25.796032+00:00 app[web.1]: ^
2019-03-19T02:16:25.796033+00:00 app[web.1]: HINT: Perhaps you meant to reference the column "patients.Full_Name".
2019-03-19T02:16:25.796035+00:00 app[web.1]: : SELECT COUNT(*) FROM "patients" WHERE (full_name LIKE '%tester%' AND user_id = 5)):
2019-03-19T02:16:25.796102+00:00 app[web.1]: F, [2019-03-19T02:16:25.796057 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec]
2019-03-19T02:16:25.796180+00:00 app[web.1]: F, [2019-03-19T02:16:25.796138 #4] FATAL -- : [75c3a8ff-f6cb-4954-8f23-071bfc2671ec] app/controllers/patients_con
troller.rb:15:in `index'
2019-03-19T02:16:25.796617+00:00 heroku[router]: at=info method=GET path="/patients?utf8=%E2%9C%93&Full_Name=tester" host=pure-reef-79084.herokuapp.com reques
t_id=75c3a8ff-f6cb-4954-8f23-071bfc2671ec fwd="95.45.117.131" dyno=web.1 connect=0ms service=16ms status=500 bytes=1827 protocol=https
the code being executed is this :
patients_controller.rb
#patients = Patient.where("patients.Full_Name LIKE :name AND patients.user_id = :id", {:name => "%#{params[:Full_Name]}%", :id => current_user.id})
I have narrowed it down to the fact that postgresql is case sensitive so i am assuming it does not like "Full_Name". I tried encapsulating the column name in double quotes like this :
#patients = Patient.where(""Full_Name" LIKE :name AND user_id = :id", {:name => "%#{params[:Full_Name]}%", :id => current_user.id})
however I get a SyntaxError. Anyone have any suggestions how i would amend this query so that it will fetch the column?
Solved
As per the answer below I had to put quotes around my column name like so :
#patients = Patient.where('"Full_Name" LIKE :name AND patients.user_id = :id', {:name => "%#{params[:Full_Name]}%", :id => current_user.id})
Your table was probably created with double quotes around the column name "Full_Name" so it expects that exact capitalization. All your references to this column should be exactly like that, not Full_Name or "full_name".
Either reference it properly, or rename your column to omit the double quotes:
ALTER TABLE "patients" RENAME COLUMN "Full_Name" TO full_name;
Good discussion here: http://blog.lerner.co.il/quoting-postgresql/

What am I missing for authenticating a user with Devise and JWT in Rails 5?

I followed this tutorial to set up authentication with JWT and devise https://www.sitepoint.com/introduction-to-using-jwt-in-rails/. My authentication_controller looks like this:
class AuthenticationController < ApplicationController
def authenticate_user
user = User.find_for_database_authentication(email: params[:email])
if user.valid_password?(params[:password])
render json: payload(user)
else
render json: {errors: ['Invalid Username/Password']}, status:
:unauthorized
end
end
private
def payload(user)
return nil unless user && user.id
{
auth_token: JsonWebToken.encode({user_id: user.id}),
user: {id: user.id, email: user.email}
}
end
end
It maps to a route called auth_user, as shown here in my routes.rb file:
Rails.application.routes.draw do
resources :reviews
resources :people do
resources :reviews
end
post 'auth_user' => 'authentication#authenticate_user'
devise_for :users, :controllers => {sessions: 'sessions', registrations:
'registrations'}
# For details on the DSL available within this file, see
http://guides.rubyonrails.org/routing.html
end
All my other routes work, including posting to the devise routes, but this one give a 500 internal server error. Any ideas of what could be causing this behavior are greatly appreciated! For what its worth, I'm on Rails 5.1.5, JWT 2.1.0, and Devise 4.4.3. Thanks!
I checked the heroku logs after trying to send a request. They look like this:
2018-04-26T20:53:32.801349+00:00 app[web.1]: I, [2018-04-26T20:53:32.801222 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Started POST "/auth_user" for 199.116.73.196 at 2018-04-26 20:53:32 +0000
2018-04-26T20:53:32.824053+00:00 app[web.1]: I, [2018-04-26T20:53:32.823923 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Processing by AuthenticationController#authenticate_user as */*
2018-04-26T20:53:32.824188+00:00 app[web.1]: I, [2018-04-26T20:53:32.824119 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Parameters: {"email"=>"\"mike#mike.com\"", "password"=>"[FILTERED]"}
2018-04-26T20:53:32.953483+00:00 app[web.1]: D, [2018-04-26T20:53:32.953329 #4] DEBUG -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] [1m[36mUser Load (8.4ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."email" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["email", "\"mike#mike.com\""], ["LIMIT", 1]]
2018-04-26T20:53:32.963709+00:00 app[web.1]: I, [2018-04-26T20:53:32.963569 #4] INFO -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] Completed 500 Internal Server Error in 139ms (ActiveRecord: 53.2ms)
2018-04-26T20:53:32.964567+00:00 app[web.1]: F, [2018-04-26T20:53:32.964492 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224]
2018-04-26T20:53:32.964715+00:00 app[web.1]: F, [2018-04-26T20:53:32.964649 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] NoMethodError (undefined method `valid_password?' for nil:NilClass):
2018-04-26T20:53:32.964805+00:00 app[web.1]: F, [2018-04-26T20:53:32.964742 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224]
2018-04-26T20:53:32.964904+00:00 app[web.1]: F, [2018-04-26T20:53:32.964844 #4] FATAL -- : [bb58b729-4d79-4940-b73d-2bc433c8d224] app/controllers/authentication_controller.rb:4:in `authenticate_user'
2018-04-26T20:53:32.966716+00:00 heroku[router]: at=info method=POST path="/auth_user" host=rateyouracquaintanceapi.herokuapp.com request_id=bb58b729-4d79-4940-b73d-2bc433c8d224 fwd="199.116.73.196" dyno=web.1 connect=1ms service=172ms status=500 bytes=203 protocol=https
Still a bit baffled as to what's breaking, but at least it's clear now where the problem is!
Change the code in authenticate_user to this:
def authenticate_user
user = User.find_for_database_authentication(email: params[:email])
if user && user.valid_password?(params[:password])
render json: payload(user)
else
render json: {errors: ['Invalid Username/Password']}, status:
:unauthorized
end
end
Now I get the expected json response for if the user was not found, but if I input the data for a valid user it does not give the proper response. Could this be a problem with the find_for_database_authentication method? The logs now show this:
2018-04-26T21:24:07.327940+00:00 heroku[router]: at=info method=POST
path="/auth_user" host=rateyouracquaintanceapi.herokuapp.com
request_id=95dcfdd7-55d5-4a24-b8e4-d597737c4b02 fwd="199.116.73.196"
dyno=web.1 connect=1ms service=15ms status=401 bytes=286 protocol=https
2018-04-26T21:24:07.319101+00:00 app[web.1]: I, [2018-04-26T21:24:07.318981
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Started POST
"/auth_user" for 199.116.73.196 at 2018-04-26 21:24:07 +0000
2018-04-26T21:24:07.320412+00:00 app[web.1]: I, [2018-04-26T21:24:07.320296
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Processing by
AuthenticationController#authenticate_user as */*
2018-04-26T21:24:07.320476+00:00 app[web.1]: I, [2018-04-26T21:24:07.320410
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Parameters:
{"email"=>"\"mike#mike.com\"", "password"=>"[FILTERED]"}
2018-04-26T21:24:07.329226+00:00 app[web.1]: D, [2018-04-26T21:24:07.328689
#4] DEBUG -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] [1m[36mUser Load
(5.1ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."email"
=
$1 LIMIT $2[0m [["email", "\"mike#mike.com\""], ["LIMIT", 1]]
2018-04-26T21:24:07.330108+00:00 app[web.1]: I, [2018-04-26T21:24:07.330005
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02]
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash
(0.17ms)
2018-04-26T21:24:07.330482+00:00 app[web.1]: I, [2018-04-26T21:24:07.330370
#4] INFO -- : [95dcfdd7-55d5-4a24-b8e4-d597737c4b02] Completed 401
Unauthorized in 10ms (Views: 1.0ms | ActiveRecord: 5.1ms)
Looking at your logs, I see:
NoMethodError (undefined method `valid_password?' for nil:NilClass):
This probably coming from the line:
user.valid_password?(params[:password])
Which means your user variable is nil. This is happening because user = User.find_for_database_authentication(email: params[:email]) is returning nil.
Check to see if your user exists in the database by running the rails console, using heroku run rails c. If not, create it, and your code should work fine.

Webpack error on Heroku Deployment

Running my rails app locally works fine. Then I deployed it to Heroku successfully but when opening the app I get the error:
"We're sorry, but something went wrong.
If you are the application owner check the logs for more information."
I'm new to using Heroku, this is my first deployed app so not completely sure of how everything works. In my heroku logs it says that:
ActionView::Template::Error (Webpacker can't find application.css in
/app/public/packs/manifest.json. Possible causes:
You want to set webpacker.yml value of compile to true for your environment
Webpack has not yet re-run to reflect updates.
You have misconfigured Webpacker's config/webpacker.yml file. unless you are using the webpack -w or the webpack-dev-server.
Your Webpack configuration is not creating a manifest.
Anyone that has had the same problem or has any tips on how to try and solve this?
This is the whole heroku log:
2017-11-19T12:30:56.336758+00:00 app[web.1]: I, [2017-11-19T12:30:56.336675 #4] INFO -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] Started GET "/" for 85.228.31.12 at 2017-11-19 12:30:56 +0000
2017-11-19T12:30:56.338912+00:00 app[web.1]: I, [2017-11-19T12:30:56.338848 #4] INFO -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] Rendered react_view/index.html.erb within layouts/application (0.7ms)
2017-11-19T12:30:56.337434+00:00 app[web.1]: I, [2017-11-19T12:30:56.337362 #4] INFO -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] Processing by ReactViewController#index as HTML
2017-11-19T12:30:56.338062+00:00 app[web.1]: I, [2017-11-19T12:30:56.337995 #4] INFO -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] Rendering react_view/index.html.erb within layouts/application
2017-11-19T12:30:56.339074+00:00 app[web.1]: I, [2017-11-19T12:30:56.339014 #4] INFO -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] Completed 500 Internal Server Error in 2ms
2017-11-19T12:30:56.339692+00:00 app[web.1]: F, [2017-11-19T12:30:56.339632 #4] FATAL -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c]
2017-11-19T12:30:56.339766+00:00 app[web.1]: F, [2017-11-19T12:30:56.339696 #4] FATAL -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] ActionView::Template::Error (Webpacker can't find application.css in /app/public/packs/manifest.json. Possible causes:
2017-11-19T12:30:56.339768+00:00 app[web.1]: 1. You want to set webpacker.yml value of compile to true for your environment
2017-11-19T12:30:56.339770+00:00 app[web.1]: 2. Webpack has not yet re-run to reflect updates.
2017-11-19T12:30:56.339771+00:00 app[web.1]: 3. You have misconfigured Webpacker's config/webpacker.yml file.
2017-11-19T12:30:56.339769+00:00 app[web.1]: unless you are using the `webpack -w` or the webpack-dev-server.
2017-11-19T12:30:56.339772+00:00 app[web.1]: 4. Your Webpack configuration is not creating a manifest.
2017-11-19T12:30:56.339974+00:00 app[web.1]: [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] 3: <%= javascript_pack_tag 'application' %>
2017-11-19T12:30:56.340038+00:00 app[web.1]: F, [2017-11-19T12:30:56.339975 #4] FATAL -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c]
2017-11-19T12:30:56.340104+00:00 app[web.1]: F, [2017-11-19T12:30:56.340050 #4] FATAL -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] app/views/react_view/index.html.erb:2:in `_app_views_react_view_index_html_erb___547572771215860275_34419720'
2017-11-19T12:30:56.339974+00:00 app[web.1]: [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] 4: <%= javascript_pack_tag 'ReactHome' %>
2017-11-19T12:30:56.339773+00:00 app[web.1]: Your manifest contains:
2017-11-19T12:30:56.339775+00:00 app[web.1]: }
2017-11-19T12:30:56.339774+00:00 app[web.1]: {
2017-11-19T12:30:56.339775+00:00 app[web.1]: ):
2017-11-19T12:30:56.339972+00:00 app[web.1]: F, [2017-11-19T12:30:56.339903 #4] FATAL -- : [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] 1: <script>window.__theme = 'bs4';</script>
2017-11-19T12:30:56.339973+00:00 app[web.1]: [0b584fb1-2c9e-41e9-bfe4-19cbf6523a6c] 2: <%= stylesheet_pack_tag 'application' %>
`
I had this error, tried to compile and also got :
$ rake assets:precompile RAILS_ENV=production
...
rails aborted!
No such middleware to insert after: ActionDispatch::Static
Solved it thanks to this thread by turning with an ENV
# config/environments/production.rb
config.public_file_server.enabled=true` # ENV['RAILS_SERVE_STATIC_FILES'].present?

Heroku 'redirected to' in log doesn't match the URL that it actually redirects to

I've just deployed my Ruby app to Heroku and the redirect_to in my contacts_controller.rb no longer works. If I run the local server, it works fine but on Heroku, it takes me to a URL that doesn't exist so I get an error message.
I've had a look at the Heroku log and even there, it says it's redirecting to the correct URL but that doesn't match what it does in the app.
I've tried restarting Heroku. I've searched through the documentation to see if there is a way to manually change the path because I noticed at one point in the log, it says it's pushing to '/contacts' which is the incorrect URL that gives me the error but I now think that's referencing the db, not the redirect URL.
What's odd is the redirect_to path is the same for success or failure in my if/else method and it works fine for the failure but not success.
contacts_controller.rb file:
class ContactsController < ApplicationController
def new
#contact = Contact.new
end
def create
#contact = Contact.new(contact_params)
if #contact.save
redirect_to new_contact_path
name = params[:contact][:name]
email = params[:contact][:email]
body = params[:contact][:comments]
ContactMailer.contact_email(name, email, body).deliver
flash[:success] = "Message sent."
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
Here's the heroku log once I've submitted my contact form:
2017-03-02T12:15:00.498443+00:00 app[web.1]: I, [2017-03-02T12:15:00.498389 #4] INFO -- : [] Redirected to https://myapp.herokuapp.com/contact-us
2017-03-02T12:15:00.506175+00:00 app[web.1]: I, [2017-03-02T12:15:00.506104 #4] INFO -- : [] Rendering contact_mailer/contact_email.html.erb
2017-03-02T12:15:00.506925+00:00 app[web.1]: I, [2017-03-02T12:15:00.506848 #4] INFO -- : [] Rendered contact_mailer/contact_email.html.erb (0.6ms)
2017-03-02T12:15:00.793142+00:00 app[web.1]: I, [2017-03-02T12:15:00.793082 #4] INFO -- : [] Sent mail to xxxxxxxx#xxxx.xxx (6.6ms)
2017-03-02T12:15:00.786369+00:00 app[web.1]: D, [2017-03-02T12:15:00.786255 #4] DEBUG -- : [] ContactMailer#contact_email: processed outbound mail in 286.1ms
2017-03-02T12:15:00.793195+00:00 app[web.1]: D, [2017-03-02T12:15:00.793141 #4] DEBUG -- : [] Date: Thu, 02 Mar 2017 12:15:00 +0000
2017-03-02T12:15:00.793197+00:00 app[web.1]: From: is
2017-03-02T12:15:00.793197+00:00 app[web.1]: To: xxxxx#xxx.xxx
2017-03-02T12:15:00.793198+00:00 app[web.1]: Message-ID: <xxxx.mail>
2017-03-02T12:15:00.793199+00:00 app[web.1]: Subject: Contact Form Message
2017-03-02T12:15:00.793200+00:00 app[web.1]: Mime-Version: 1.0
2017-03-02T12:15:00.793200+00:00 app[web.1]: Content-Type: text/html;
2017-03-02T12:15:00.793201+00:00 app[web.1]: charset=UTF-8
2017-03-02T12:15:00.793201+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
2017-03-02T12:15:00.793202+00:00 app[web.1]: <!DOCTYPE html>
2017-03-02T12:15:00.793202+00:00 app[web.1]:
2017-03-02T12:15:00.793203+00:00 app[web.1]: <html>
2017-03-02T12:15:00.793204+00:00 app[web.1]: <head></head>
2017-03-02T12:15:00.793204+00:00 app[web.1]: <body>
2017-03-02T12:15:00.793205+00:00 app[web.1]: <p>You have received a message from the site's contact form, from
2017-03-02T12:15:00.793206+00:00 app[web.1]: this, is.</p>
2017-03-02T12:15:00.793207+00:00 app[web.1]: <p>frustrating</p>
2017-03-02T12:15:00.793208+00:00 app[web.1]: </body>
2017-03-02T12:15:00.793208+00:00 app[web.1]: </html>
2017-03-02T12:15:00.793409+00:00 app[web.1]: I, [2017-03-02T12:15:00.793333 #4] INFO -- : [] Completed 500 Internal Server Error in 305ms (ActiveRecord: 3.8ms)
2017-03-02T12:15:00.793869+00:00 app[web.1]: F, [2017-03-02T12:15:00.793819 #4] FATAL -- : []
2017-03-02T12:15:00.793910+00:00 app[web.1]: F, [2017-03-02T12:15:00.793871 #4] FATAL -- : [] Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
2017-03-02T12:15:00.793958+00:00 app[web.1]: F, [2017-03-02T12:15:00.793909 #4] FATAL -- : []
2017-03-02T12:15:00.793979+00:00 app[web.1]: F, [2017-03-02T12:15:00.793943 #4] FATAL -- : [] app/controllers/contacts_controller.rb:13:in `create'
2017-03-02T12:15:00.794339+00:00 heroku[router]: at=info method=POST path="/contacts" host=myapp.herokuapp.com request_id=25154829-90fe-44c4-a715-eaf9a6fd83d1 fwd="87.114.208.14" dyno=web.1 connect=1ms service=313ms status=500 bytes=1669
And the actual link that it redirects me to is https://myapp.herokuapp.com/contacts
Error message on screen: "We're sorry, but something went wrong.
If you are the application owner check the logs for more information."
I have a feeling it might be something to do with my db but I have no idea why it's redirecting to that link. The db that I've migrated to Heroku is called 'contacts'
Really appreciate any suggestions, thank you.
Your issue seems to be with email delivery, not redirecting. If you take a look again at your log, you will see the following lines:
2017-03-02T12:15:00.793409+00:00 app[web.1]: I, [2017-03-02T12:15:00.793333 #4] INFO -- : [] Completed 500 Internal Server Error in 305ms (ActiveRecord: 3.8ms)
2017-03-02T12:15:00.793869+00:00 app[web.1]: F, [2017-03-02T12:15:00.793819 #4] FATAL -- : []
2017-03-02T12:15:00.793910+00:00 app[web.1]: F, [2017-03-02T12:15:00.793871 #4] FATAL -- : [] Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
Your application is returning a 500 error, and the error message shown in the log indicates an issue with email delivery (email via SMTP uses port 25).
Do you have an email add-on installed and configured for your Heroku account and attached to your app? There are several listed on Heroku's website: https://elements.heroku.com/addons/categories/email-sms
I suspect this is working as intended locally because you are running software such as postfix, but this is not present on Heroku out-of-the-box.

Resources