Recaptcha rails 5 error - ruby-on-rails

I am having an error with recaptcha recognizing my keys in secrets.yml in production. In development, everything works fine!
I was able to upload successfully my site using capistrano and nginx, I placed the recaptcha keys in secrets.yml but I get the following error in rails logs.
[70624ace-d7c2-41d4-a312-81e558237559] app/views/contacts/_new.html.erb:1:in `_app_views_contacts__new_html_erb___113748072819217608_34958780'
[70624ace-d7c2-41d4-a312-81e558237559] app/views/welcome/index.html.erb:97:in `_app_views_welcome_index_html_erb__2483501284442413835_31069860'
I, [2018-02-13T15:06:06.655443 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Started GET "/" for 66.249.70.22 at 2018-02-13 15:06:06 +0000
I, [2018-02-13T15:06:06.656262 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Processing by WelcomeController#index as HTML
I, [2018-02-13T15:06:06.657082 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Rendering welcome/index.html.erb within layouts/application
I, [2018-02-13T15:06:06.666156 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Rendered contacts/_new.html.erb (8.3ms)
I, [2018-02-13T15:06:06.666316 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Rendered welcome/index.html.erb within layouts/application (9.1ms)
I, [2018-02-13T15:06:06.666485 #23369] INFO -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] Completed 401 Unauthorized in 10ms
F, [2018-02-13T15:06:06.667938 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f]
F, [2018-02-13T15:06:06.668014 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] ActionView::Template::Error (No site key specified.):
F, [2018-02-13T15:06:06.668157 #23369] FATAL -- : [1abb6e09-8ebd-41a3-ad0c-ea88749d804f] 9:
I am trying to follow this tutorial https://github.com/ambethia/recaptcha, but it does not provide much detail about placing the keys in production.
I have tried many things, like adding export in front of the keys, placing them in different files, and checking secrets.yml for any syntax error. I have also made sure that I am using RECAPTHCA_SITE_KEY instead of RECAPTCHA_PUBLIC_KEY.
Also, I have placed my domain in recaptcha's site.
I would appreciate if you could help me solve the problem as well as provide some explanation of why I was having the error.
This is my secrets.yml in production:
production:
secret_key_base: ...SECRET_KEY...
RECAPTCHA_SITE_KEY: "...SITE_KEY..."
RECAPTCHA_PRIVATE_KEY: "...PRIVATE_KEY..."
Thanks!

You need to configure recaptcha to pull the keys from your secrets.yml by adding an initializer file, config/initializers/recaptcha.rb:
# config/initializers/recaptcha.rb
Recaptcha.configure do |config|
config.site_key = Rails.application.secrets[:RECAPTCHA_SITE_KEY]
config.secret_key = Rails.application.secrets[:RECAPTCHA_PRIVATE_KEY]
# Uncomment the following line if you are using a proxy server:
# config.proxy = 'http://myproxy.com.au:8080'
end

Related

Rails redirect loop in production, everything is fine in development

I am running into an issue with Rails where in production it generates an error in the log file, but nothing in the development mode:
I, [2019-10-16T12:02:16.953954 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Started GET "/" for 155.186.157.103 at 2019-10-16 12:02:16 -0500
I, [2019-10-16T12:02:16.955549 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Processing by Devise::SessionsController#new as HTML
I, [2019-10-16T12:02:16.956733 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Rendering devise/sessions/new.html.erb within layouts/application
I, [2019-10-16T12:02:16.958443 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Rendered devise/shared/_links.html.erb (0.3ms)
I, [2019-10-16T12:02:16.958796 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Rendered devise/sessions/new.html.erb within layouts/application (2.0ms)
E, [2019-10-16T12:02:29.471353 #11866] ERROR -- : [2c2037b2-3b07-495d-b936-778c070e1ad7] undefined method `[]' for nil:NilClass
I, [2019-10-16T12:02:29.473147 #11866] INFO -- : [2c2037b2-3b07-495d-b936-778c070e1ad7] Redirected to [redacted-for-stackoverflow]
I, [2019-10-16T12:02:29.473928 #11866] INFO -- : [2c2037b2-3b07-495d-b936-778c070e1ad7] Completed 302 Found in 22438ms (ActiveRecord: 0.0ms)
E, [2019-10-16T12:02:39.225917 #11866] ERROR -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] undefined method `[]' for nil:NilClass
I, [2019-10-16T12:02:39.227867 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Redirected to [redacted-for-stackoverflow]
I, [2019-10-16T12:02:39.229075 #11866] INFO -- : [e5d5e3b6-2a9c-4d27-b1f3-2d5a1b76ce5a] Completed 302 Found in 22273ms (ActiveRecord: 0.0ms)
However, if I load development mode, the login page comes up just fine. I can't figure out what could possibly be causing this error since the error isn't descriptive as to where the issue resides.
I ran across another post to temporarily change config.consider_all_requests_local = false to config.consider_all_requests_local = true in config/environments/production.rb but that hasn't changed anything. No additional information is being provided.
How can I trace down this issue that's only occurring in production?
I have literally replaced everything in layouts/application.html.erb to try pin pointing where the issue is, but no luck. I noticed that rake assets:precompile fails as well with the same error, so perhaps it has some kind of issue in there.
Can you show the contents of devise/sessions/new.html.erb
It seems you are attempting to access a hash by key e.g. my_hash[:my_key] but my_hash in nil
After troubleshooting this some more, it finally boiled down to a stylesheet ending in .css rather than .css.scss since it was loading some rails stuff. I only came across this because rake assets:precompile kept failing.

ArgumentError (Signing key must be provided) in Rails + Carrierwave + Microsoft Azure

I've been doing some coding practice and i need to know how to provide the necessary credentials to my app in order to stablish a connection to Microsoft Azure and upload images in my app running in production with Heroku.
I'm using CarrierWave gem as The Rails tutorial shows to upload images in production. I'm using Microsoft Azure instead of using AWS and my carrier_wave.rb is structured like this:
if Rails.env.production?
require 'azure/storage/blob'
blob_client = Azure::Blob::BlobService.
CarrierWave.configure do |config|
blob_client = Azure::Storage::Blob::BlobService.create(
storage_account_name: my_account_name,
storage_access_key: my_account_key
)
end
end
But when i push to Heroku and i try to upload a image in my app it crashes
The logs here:
2019-02-23T04:01:46.917324+00:00 app[web.1]: D, [2019-02-23T04:01:46.917249 #10] DEBUG -- : [d1de016d-29a7-4338-b6f0-071aced60e70] (0.6ms) ROLLBACK
2019-02-23T04:01:46.917957+00:00 app[web.1]: I, [2019-02-23T04:01:46.917872 #10] INFO -- : [d1de016d-29a7-4338-b6f0-071aced60e70] Completed 500 Internal Server Error in 112ms (ActiveRecord: 5.2ms)
2019-02-23T04:01:46.918790+00:00 app[web.1]: F, [2019-02-23T04:01:46.918718 #10] FATAL -- : [d1de016d-29a7-4338-b6f0-071aced60e70]
2019-02-23T04:01:46.918843+00:00 app[web.1]: F, [2019-02-23T04:01:46.918781 #10] FATAL -- : [d1de016d-29a7-4338-b6f0-071aced60e70] ArgumentError (Signing key must be provided):
2019-02-23T04:01:46.918891+00:00 app[web.1]: F, [2019-02-23T04:01:46.918837 #10] FATAL -- : [d1de016d-29a7-4338-b6f0-071aced60e70]
2019-02-23T04:01:46.918942+00:00 app[web.1]: F, [2019-02-23T04:01:46.918891 #10] FATAL -- : [d1de016d-29a7-4338-b6f0-071aced60e70] app/controllers/books_controller.rb:22:in `create'
I've followed examples of how provide the information needed from Microsoft Docs but it's seems i'm not doing the integration properly. I would appreciate the help in this question.
From where are you getting my_account_name and my_account_key? are you sure there are valid and has value?
According to BlobService#initialize -> SharedKey#initialize -> Signer#initialize
your acces_key is nil
# Initialize the Signer.
#
# #param access_key [String] The access_key encoded in Base64.
def initialize(access_key)
if access_key.nil?
raise ArgumentError, 'Signing key must be provided'
end
#access_key = Base64.strict_decode64(access_key)
end

Authenticated requests for an Rails API backend and React frontend fail when app deployed to Heroku

I have an app which uses Rails API backend and React frontend. It works locally but after I deployed it to Heroku all the requests that require authentication fail and give
Error: "Token is invalid"
I have checked the headers and the token is there. Can't figure out by looking at Heroku logs what the problem is.
Did someone have a similar issue?
Thanks!
A snippet from Heroku logs:
2017-10-26T17:56:36.419481+00:00 app[web.1]: I, [2017-10-
26T17:56:36.419403 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Parameters: {"auth"=>{}}
2017-10-26T17:56:36.420592+00:00 app[web.1]: I, [2017-10-
26T17:56:36.420511 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] [active_model_serializers] Rendered
ActiveModel::Serializer::Null with Hash (0.18ms)
2017-10-26T17:56:36.421050+00:00 app[web.1]: I, [2017-10-
26T17:56:36.420931 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Filter chain halted as :authenticate_token! rendered or
redirected
2017-10-26T17:56:36.421378+00:00 app[web.1]: I, [2017-10-
26T17:56:36.421175 #4] INFO -- : [bc68efaf-22e2-4274-92d8-
d1202d2b1228] Completed 403 Forbidden in 2ms (Views: 1.1ms |
ActiveRecord: 0.0ms)
Heroku has settings where you can define variables. It's possible you have a .env variable or something similar that needs to be defined there, because Heroku isn't recognizing it.

Rails: Assets cannot be found in production mode, although being served properly by caddy

I have been trying to fix this problem for almost days now.
I am trying to run my ruby on rails site in production mode, but (some of) the assets cannot be served. The images are located in a custom subfolder ( app/assets/audio/testfolder/demo.png ) which is added to Rails.application.config.assets.paths and precompiled properly but I am always getting the error
ActionView::Template::Error (The asset "demo.png" is not present in the asset pipeline.)
or to be more specific:
I, [2017-09-25T00:38:32.859541 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Started GET "/" for 127.0.0.1 at 2017-09-25 00:38:32 +0200
I, [2017-09-25T00:38:32.860377 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Processing by TestControllerController#index as HTML
I, [2017-09-25T00:38:32.861240 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Rendering test_controller/index.html.erb within layouts/application
I, [2017-09-25T00:38:32.861558 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Rendered test_controller/index.html.erb within layouts/application (0.1ms)
I, [2017-09-25T00:38:32.863790 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Completed 500 Internal Server Error in 3ms
F, [2017-09-25T00:38:32.864505 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1]
F, [2017-09-25T00:38:32.864789 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] ActionView::Template::Error (The asset "demo.png" is not present in the asset pipeline.):
F, [2017-09-25T00:38:32.865058 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] 10:
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 11: <body>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 12: <%= yield %>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 13: <%= image_tag(yield(:img)) %>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 14: <audio src="<%= audio_path(yield(:audio))%>" type="audio/mpeg" controls preload="none"></audio>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 15: </body>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 16: </html>
The image can be accessed by localhost:81/assets/demo-57cec01fc3b0b102085dc4de14576f8fd86f8298aed121ce17245df0546276d4.png directly, so the webserver (caddy) does not seem to be the problem.
I hosted a simple demo project on github ( https://github.com/Kafkalasch/RailsAssetsErrorDemo ) to reproduce the problem easily.
Just clone the project, precompile the assets
rails assets:precompile
, start the caddy server with the given Caddyfile,
caddy -conf Caddyfile
and start puma with config/puma.production.rb
puma -C RubyDemo/config/puma.production.rb
I am using:
ruby 2.4.1
puma 3.10.0
rails 5.1.4
caddy 0.10.6
I could reproduce this problem on Debian and Mac OS X, so I don't assume that the OS is relevant.
I want to use my webserver to serve static files, so I am looking for an other solution than simply setting
config.assets.compile = true
Does anyone have an idea or any hint how I could solve my problem?
I am grateful for any response.
I tested your app with puma webserver in production and development.
I solved the error by including the folder for the mp3 and png files in your index.html.erb file.
<% provide(:img, "testfolder/demo.png") %>
<% provide(:audio, "testfolder/demo.mp3") %>
because your images and assets are precompiled inside public/assets/testfolder instead of public/assets
The problem is solved

rails Dir working local, not on heroku

In a rails app, if I am working locally, I can read a folder, with its own subfolders and files. in Heroku is not working. any help?
def upload_multifiles
dir = params[:path]
files_directories = Dir["#{dir}/**/*"]
files = []
files_directories.each do |file_directory|
if file_directory.include? ".pdf"
files << file_directory
end
end
end
the error that i get is:
2017-08-07T08:23:06.984190+00:00 app[web.1]: I, [2017-08-07T08:23:06.984142 #4] INFO -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.6ms)
2017-08-07T08:23:06.983899+00:00 app[web.1]: found_dir
2017-08-07T08:23:06.984787+00:00 app[web.1]: F, [2017-08-07T08:23:06.984747 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] app/controllers/multiuploader_controller.rb:14:in `open'
2017-08-07T08:23:06.984642+00:00 app[web.1]: F, [2017-08-07T08:23:06.984593 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8]
2017-08-07T08:23:06.984756+00:00 app[web.1]: F, [2017-08-07T08:23:06.984698 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8]
2017-08-07T08:23:06.984697+00:00 app[web.1]: F, [2017-08-07T08:23:06.984647 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] Errno::ENOENT (No such file or directory # dir_initialize - /Users/xxxxx/yyyyy/ttttt/zzzzzz):
2017-08-07T08:23:06.984788+00:00 app[web.1]: [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] app/controllers/multiuploader_controller.rb:14:in `upload_multifiles'
Thanks a lot in advance
Albert
You can not use the File system on heroku like you can do on your lolcal machine. You will need to use something like S3 if you want to store files (from uploads).
You can read more about it here: How to use heroku's ephemeral filesystem
If you just want to read files that you have shipped with your app: the path /Users/xxxx is a OS X directory structure, which does not exist on heroku. You could use Rails.root to build paths that work in all environments.
In addition: listing files from a path supplied through parameters sounds like a huge security risk to me. You should not do this.
Also: there is a more elegant way to create a filtered array, for example:
files = ['a.pdf', 'b.pdf', 'c.txt']
pdfs = files.select { |file| file.ends_with?('.pdf') }

Resources