How to Run a Job Manually

If you want to trigger a background job manually, all you need to do is:

(1) rails c

(2) name_of_job.perform_now(params, if any, else just use ())

For example, if your job is Orders::SendEmailJob, then Orders::SendEmailJob.perform_now()

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.

Measuring Test Coverage with Simplecov

I’m using the awesome Simplecov gem to see where I stand with test coverage within my apps.

It’s very simple to use:

(1) install the gem:

gem 'simplecov', require: false, group: :test

(2) include the gem in your test suite (for me that meant at the very top of test/test_helper.rb):

require 'simplecov'
SimpleCov.start

(3) run your tests

# for example
bin/rails test

(4) and then, just open the .index html file in /coverage

How to View Localhost on your Mobile Device

To test something running locally on your computer, you can use ngrok.

I’m using a Windows PC, so you YMMV if you are using a Mac.

  1. I installed Chocolatey
  2. Run ‘choco install ngrok.portable’ in Powershell or CMD
  3. After the installation is complete, run ‘ngrok http 3000’ (my Rails app is running on port 3000)
  4. Ngrok should then show a URL that maps to localhost:3000
  5. Enter the URL on your mobile device

Voila! You should be running your Rails app on your mobile device now.

How to Seed Rails DB Using CSV

(1) put your CSV file in /lib

(2) change your seeds.rb file in /db

require 'csv'

CSV.foreach(Rails.root.join('lib/teams.csv'), headers: true, encoding:'iso-8859-1:utf-8') do |row|    
  YOUR_MODEL_NAME.create({    
    FIELD_NAME: row[0],
    FIELD2_NAME: row[1]
  })
end

(3) run ‘rake db:seed’

Separating Tags in a Text Field

I’m using the awesome acts-as-taggable-on gem to implement tagging. One problem that I ran into is that when users come back to the edit page, and you use the standard coding for a field, such as this:

<%= f.text_field :tag_list %>

Then the values show, but they’re not comma separated. The big problem with that is that if the users save the form again and don’t add commas, the values will change.

For example, if the person originally had this in the tag list: “funny,exciting,premium”, then in the edit view, the field would read in as “funny exciting premium”, and if they saved, instead of three different tags, they would now have one tag: “funny exciting premium” which isn’t what anyone wants.

I found a great solution on SO by DaniG2k (assuming that the list for your tags is called “tagged list”:

<% @article.tag_types.each do |tag| %>
<div class="form-group">
  <strong><%= f.label tag.to_s.titleize %></strong><br />
  <% tag_sym = "#{tag.to_s.singularize}_list".to_sym %>
  <% tag_list = "#{tag.to_s.singularize}_list" %>
  <%= f.text_field tag_sym, value: @article.send(tag_list).to_s, :placeholder => "Comma-separated list of #{tag.to_s}", class: 'form-control' %>
</div>
<% end %>

Don’t forget to change @article to whatever model you’re working with.

Webpacker not Updating on Windows (using WSL)

I’m using WSL to use Linux on my machine. Sometimes when there’s JS changes, they don’t work until I run:

./bin/webpack

then yarn install

Usually, I use Ubuntu to run my code, but I sometimes run into errors such as a permission error when i run ./bin/webpack. Sometimes, I don’t see an error at all when running ./bin/webpack.

The workaround I’ve found is:

(1) opening CMD as admin

(2) going to the directory with my rails code

(3) on the command line entering ‘WSL’ (then enter)

(4) ./bin/webpack (then enter)

(5) yarn install (then enter)