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()
Follow Along as I Stumble on the Path to Learning RoR
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()
Let’s say the attribute student
in User
is a boolean,
To check if it’s true, you can do: if @user.student
To check if something is false, you can do: if !@user.student
Sometimes when I’m running a Rails app after changes, the CSS doesn’t load correctly.
When that’s the case, sometimes it gets fixed with these two commands to manually update the assets:
rake assets:clobber
rake assets:precompile --trace
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.
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
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.
Voila! You should be running your Rails app on your mobile device now.
(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’
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.
I wanted to show a user’s local time when displaying message, so the time made more sense to them.
I was able to do this easily using Basecamp’s Local Time gem.
The gem was built when the Asset Pipeline was used for Javascript, but it was easy to adjust. All I needed to do was run yarn install local-time
.
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)