Form Errors not Showing

I had a problem with form errors not showing.

When a form error occured (upon submission), the page would reload with all of the fields blank and the section that shows the form errors didn’t appear (so the user has no idea what happened and what to fix).

I found out that it was because Turbo wasn’t playing nice with Cloudflare’s Rocket Loader.

If you use Cloudflare and are having this issue, then try disabling Rocket Loader.

Fixing Rails’ “undefined method `map’ for nil:NilClass” errors for collection_selects

I had a weird problem. I defined a collection_select in my form, something like this:

<%= f.collection_select :campaign_id, @campaigns, :id, :title %>

When I went to the form, the page loaded, and the select populated correctly. However, when I submitted the form, I would get the “undefined method `map’ for nil:NilClass” error.

By mistake, I realized that the sometimes the form posted and sometimes it didn’t. Then I realized that there were Active Record validation errors with the submitted form, but the page wasn’t showing, so I couldn’t see that these errors occurred (even while looking at the logs).

I had to search how to fix this, and eventually found this Stack Overflow question.

The solution by Wes Foster, worked for me to ensure that the collection was getting determined when the new action was rendered (after the record didn’t create).

class MyController < ApplicationController
  before_action :set_select_collections, only: [:edit, :update, :new, :create]

  private
    def set_select_collections
      @campaigns = current_user.business.campaigns
    end
end

Incidentally, if you’re receiving this error and your form isn’t even loading, it probably means that you didn’t define the collection value in your controller (@campaigns in this example).

Showing Errors in the Logs

I had some errors occurring in my controller, but it wasn’t displaying in my views.  So I had to figure out how to see what was happening since I didn’t see any messages in the logs that were helpful either.

I found the solution in one of the answers here on Stack Overflow.

By adding this to my controller (it was the creation of a model – @user):

logger.warn("====error==========#{@user.error.full_messages.inspect}============")

This recorded the error in my logs (one of my model validations needed to be changed).

The plain HTTP request was sent to HTTPS port

I was receiving the error “The plain HTTP request was sent to HTTPS port” error”, Nginx error 400.  The problem was a an Nginx configuration error.  I found the solution in this StackOverflow question.  In the config file with your server info (mine was in /etc/nginx/sites-enabled/default):

make these changes:

listen 80;
  listen 443 default ssl;

  # ssl on   - remember to comment this out

 

Creating a CSR on Windows

In order to create a SSL Certificate, you need to generate a CSR, or Certificate Signing Request.  I ran into some issues when generating one from Windows, so I wanted to document the steps that I took.

  1. went to http://slproweb.com/products/Win32OpenSSL.html to download Win32 OpenSSL v1.0.0k
  2. installed OpenSSL
  3. I received an error regarding C++ during the install, so I installed Visual C++ 2008 Redistributables from the same page I downloaded OpenSSL
  4. Run a command prompt (cmd.exe from the search box in the Windows start menu)
  5. When I tried to run an OpenSSL command, I received an error that a file could not be found (WARNING: CAN’T OPEN CONFIG FILE: /USR/LOCAL/SSL/OPENSSL.CNF)
  6. As noted here, you have to set the environmental variable by typing this into the cmd: set OPENSSL_CONF=c:\[PATH TO YOUR OPENSSL DIRECTORY]\bin\openssl.cfg
  7. navigate to the bin directory
  8. enter your openssl command to generate your csr.  For me, it was:
    openssl req -nodes -newkey rsa:2048 -keyout myserver.key -out myserver.csr
  9. this created key and csr files I could use for creating my SSL certificate

Error From Coffee Files

When I had to set up a new Windows PC with Rails, I ran in to a mysterious error where I couldn’t view my app because of purported problems with my .coffee.js files.  However, I knew there was no issue with the files since they were created during scaffolding and had only comments in them.  Furthermore, I was able to run the same code on my other PC fine, as is.

When I viewed the –trace there was nothing of note (except listing that the coffee files had issues with them).  When I tried to load the view, I did see this though “ExecJS::RuntimeError in Home#index”

Fortunately, I found the solution in this thread which explains that this is the result of Node.js not on a PC, or the path not set correctly in Windows environment variables.  I downloaded the Windows installer (.msi) file and installed Node.js and added the path to the PATH environment variable.  After I restarted my Rails server, the error went away.

Creating a Manifest.json file for the Google Chrome Store in Windows

I ran into an issue with my manifest.json file when trying to submit my app to the Google Chrome Store.  I followed the directions, but apparently there was a detail that was so simple, it wasn’t documented.  The documentation says to create a text file to put the manifest code, and then to save it as manifest.json.  However, when I loaded my zip file, I kept receiving error messages saying that the manifest.json was not at the root level and couldn’t be found.  The only thing was the file was of course there, and there were no subdirectories in my zip file.

It turned out to be a stupid, basic mistake.

When I looked at the properties of the file, the file was a .txt file, and the full name was actually manifest.json.text even though I couldn’t see the .txt part in Windows Explorer.  I found the solution was that when the file was created, I should have named it manifest.json, but also when I saved it in Notepad, I should have selected “Save as type” as “All Files”.  When you look at the properties, the file type will now be “json” instead of “txt”.

Rails Paperclip Error: identify ‘-format’ ‘%wx%h’

I was receiving the “identify ‘-format’ ‘%wx%h’ ” error with Paperclip.  The reason is because Paperclip requires ImageMagick if images are manipulated, and the path must be specified for this to happen.  In order to find the path in Windows, what I did was:

  1. open a command prompt (CMD from the start menu)
  2. navigate to c://
  3. type “PATH”
  4. it should show the path(s) that can be used for ImageMagick
  5. I added this line to development.rb

Paperclip.options[:command_path] = “/bin” #(in place of “/bin” put your path)

Restart your server.

Sign Out Wasn’t Working with Devise for Me

Devise’s Sign Out wasn’t working for me. I’ve seen other solutions, but they didn’t worked for me. However, I found an answer that worked for me on Devise’s wiki.

Previously, I used this link:

<%= link_to "Logout", destroy_user_session_path, method: :delete %>

However, that wasn’t working, so I changed (in config/initializers/devise.rb in ):

config.sign_out_via = :delete

to:

config.sign_out_via = :get

and revised the link to (removed “method: :delete”):

<%= link_to "Logout", destroy_user_session_path %>