How to add a period after a link in HAML

Getting back into the hang of HAML with my new full-stack position at Orbit. I will probably write a bunch of small tips I learn along the way — mostly so I can have a place to reference them!

Adding a line of only a period does not work well in haml.

= link_to "click link", "#", target: :_blank, rel: :noreferrer 
.

This code returns: Illegal element: classes and ids must have values

= link_to "click link", "#", target: :_blank, rel: :noreferrer 
\.

This snippet does not produce an error, but it gives us a blank space between the text and the period. click link .

= link_to("click link", "#", target: :_blank, rel: :noreferrer) + "."

Concatenating a period onto the link text works perfectly, just remember to add the parentheses to the link_to args so it doesn’t treat the + "." as an invalid argument.

Sending JSON as an attribute in a RSpec Request Test

I haven’t posted in quite a while. Been working with a lot of RSpec request testing for controllers, graphql, and active admin. (Never forget the S in RSpec is capitalized – that would have saved me a lot of pain!!)

My current trouble was sending an attribute that required a json value to an active admin create route via RSpec Request. I tried a whole bunch of things, but the solution is sooo simple, (too simple for the time spent on it) that I had to post it in case I needed to look up what I did again.

I just had to add .to_json on that specific attribute requiring json.

  let(:valid_attributes) do
    attributes_for(
      :model,
      content: {"key"=>"value"}.to_json
    )
  end

I hope this helps someone 🙂

I need to start posting more, just when you find the solution after debugging for so many hours it feels too simple to post about. Like I should have known that already! But I think we all have those moments!

Happy Programming 🙂

How View Partials Have Saved Me So Much Time

Since having made so many partials to may app upon creation, it has saved me so much time in updating it, that I felt I should take a few minutes that I gained to write a post on how useful it is, so maybe you will utilize this super easy and handy tool.

On my Recipe App that I made on Ruby on Rails, anything that can be used in multiple places were rendered as a partial.

These same lines were rendered on user show pages, recipe show pages, and index pages. In fact my smallest partial was simply an image of a heart!

Aside from all the work in getting my app to work, and function correctly, have links with proper routes and forms that would submit, I would only have to worry about it once, because it was only in my views once — they were using the same partial.

And when I later implemented JavaScript and Jquery and hijacked links and forms, when I did it in one place, it worked on the whole site. It’s tough enough working with AJAX requests and JQuery/Javascript functions, I don’t need the extra work by having to enter many views to make sure classes and ID’s are always correct.

Here is an example of a partial that was used:

Recipe#Show

1
2
3
4
5
6
7
8
9
<%= render 'recipes/recipe_details', recipe: @recipe %>
<div>
<%= render 'comments/list_comments', comments: @recipe.comments if @recipe.comments %>
</div>
<br />
<div id="comments">
Add your comment below! (You must be signed in)
<%= render 'comments/form', comment: @recipe.comments.new, recipe: @recipe%>
</div>

We’ll follow the Recipe Details to recipe_details:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<h1><%= recipe.name %></h1>
<h2><%= recipe.time_in_minutes %> minutes until it's ready | By: <%= link_to recipe.user_name, user_path(recipe.user)%></h2>

<%= render 'favorites/favorited', recipe: recipe%>

<%= render 'ingredients/list_ingredients', recipe: recipe %>

<h4><%= recipe.instructions %></h4>

  <%= link_to "Edit", edit_recipe_path(recipe) if logged_in? && current_user.can_edit?(recipe)%>

  <%= link_to "Delete", recipe_path(recipe), method: :delete if logged_in? && current_user.can_delete?(recipe)%>

Which then pulls in 2 more partials, one for the favorite icon (our smallest partial), and one for ingredients:

favorites/favorited partial:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<% if current_user && recipe.favorite?(current_user) %>
  <span class="favorites favorited">
    <%= link_to image_tag("heart-red.png"), "#", :data => {:recipe => recipe.id }%>
    
  </span>
<% else %>
  <span class="favorites favorite">
    <%= link_to image_tag("heart.png"), "#", :data => {:recipe => recipe.id }%>
  </span>
<% end %>

ingredients/list_ingredients partial:

1
2
3
4
5
<ul>  
  <% recipe.recipe_ingredients.each do |recipe_ingredient| %>
    <li><%= "#{recipe_ingredient.quantity} #{recipe_ingredient.ingredient.name}" %></li>
  <% end %>
</ul>

I hope this will inspire you to keep making partials as details as necessary!

Heroku Deploy with Rails App

With my recent rails project at the Flatiron School, I deployed to heroku and learned a few cool things.

I found this video extremely helpful for the deploy, and set up of postgres (and you get a funny blooper at the end!):

Really useful advice I was given with heroku was to deploy early, and frequent so any bugs can be fixed as they happen instead of having to figure them out and make major changes because the bug is so embedded in the app that was working locally.

If you don’t want things going live immediately you can add a staging environment – https://devcenter.heroku.com/articles/multiple-environments.

You run heroku rake db:migrate, heroku rake db:seed to set up your database and seed your data.

You cannot drop a database in heroku, like you would in rails so you need to do heroku pg:reset

To drop into the rails console run: heroku run rails console. I found this super helpful to edit existing data in my databases, and run database queries.

You can set up configed vars for private keys and the like:

$ heroku config:set GITHUB_USERNAME=joesmith
Adding config vars and restarting myapp... done, v12
GITHUB_USERNAME: joesmith

$ heroku config
GITHUB_USERNAME: joesmith
OTHER_VAR:    production

$ heroku config:get GITHUB_USERNAME
joesmith

$ heroku config:unset GITHUB_USERNAME
Unsetting GITHUB_USERNAME and restarting myapp... done, v13

https://devcenter.heroku.com/articles/config-vars

And of course you’ll need to run heroku logs when you need to troubleshoot why something is not working as you’d expect.

UPDATE:  I just switched to a new computer and heroku did not recognize git push heroku master. It threw this nasty error at me:

fatal: ‘heroku’ does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Here’s the fix! heroku git:remote -a <yourapp> 

Then all works fine!

 

Happy Deploying!

 

Copyright Loving to Code 2025
Tech Nerd theme designed by Siteturner