The Blog
Sometimes we write stuff.
You can read it here now, or later with the RSS feed,
or Subscribe to our (almost weekly) newsletter.
-
· Barry Hess · TIL
TIL: Merge nested attributes in Rails permit params
I am working on a new product that's a bit more complicated than our prior releases. Getting refamiliarized with nested parameters in Rails has been interesting. In this case imagine I have a discussion that, when creating it I also want to create the first comment. The discussion information is in a form with a nested form for the comment. So at the base you have these permitted parameters:
params.require(:discussion).permit(:title, comments_attributes: [:body])
That is fine, but I wanted to assure that the discussion and comment couldn't be messed with and would always be associated with the logged in,
current_user
. Stack Overflow told me this would be pretty straightforward these days. Unfortunately that didn't work for me. Perhaps I'm doing something wrong, but I had to get a little more…into it.def discussion_params
_discussion_params = params.require(:discussion).permit(:title, comments_attributes: [:body])
_discussion_params.reverse_merge!(user_id: current_user.id)
_discussion_params[:comments_attributes].each do |key, value|
_discussion_params[:comments_attributes][key] = value.reverse_merge(user_id: current_user.id)
end
_discussion_params
endI'd be happy to hear that there's an easier way.
-
· Barry Hess · TIL
TIL: JavaScript Turbo Stream Requests
I am working on search for Album Whale and I was very confused when my JavaScript fetch requests were not resulting in turbo_stream rendering getting displayed on my page. I checked my logs to see if the render was happening. I looked for missing
<%=
tags. Debugged andlogger.info
'd all over.Then I finally noticed the following in my Rails logs:
Processing by AlbumsController#search as */*
Oh, I expected that to say something about
TURBO_STREAM
, like:Processing by AlbumsController#search as TURBO_STREAM
This sent me on a dive into headers (I didn't have an
Accept
header on myfetch
request), response handling, and the fetch API.Here's what I came up with for our first rough cut of a working, live search field (this is Stimulus code):
-
Season 3, Issue 1: Whale, Hello There!
Dear friends, we have not been good at writing this newsletter regularly. But writing is hard work, and there’s only so much time we have, and there was the winter break and Shawn kept getting sick—we have many excuses, how much time do you have?
-
All About CSS: Alphabetize, Normalize, and Dark-mode-itize
Hello reader, I’m Matthew, the newest member of Good Enough (LLC). The rest of the team are avid writers and sharers… I’m not. But they keep chanting “One of us. One of us.” so I suppose you’ll see me around here sometimes.
For my inaugural post, I thought I’d quickly share what I did in my first few days to win their affection.
-
· Barry Hess · TIL
TIL: Rails Testing
I had to figure out how to run browser-based tests in Rails again. In part that meant working around my lack of Chrome. Rails System Tests with Safari.
I also had some problems with Rails Turbo Drive taking over EVERYTHING, which required me to figure out how to deal with a Rails form redirect not rendering HTML.
-
TIL: Victory Symbol
We noticed that the victory symbol at the bottom of our homepage looked different in Chrome than Safari––it was playing smart and rendered the unicode character as an emoji. Lettini found a fix though! Append this string of code to the symbol:
︎
.So
✌︎
renders to ✌︎ and not emoji. Weird.(Here’s a blogpost that explains it all.)
-
· Barry Hess · TIL
TIL: Turbo Frames
DoEvery.Day calendar navigation was broken. We built it using Turbo Rails as it was being developed. As we have updated libraries, including Rails, the API has changed. In order to get the calendar navigation working again, I had to pull out some old hacks that made use of
turbo_stream_action_tag
via Turbo Streams to get my Turbo Frames working. The code is a bit more straightforward now, though I find Turbo Frames to feel a little too magical.Turbo Frames Vs. Turbo Streams gave me my first hints at how to fix things up, but the example request flow was a little too odd for me to absorb. Turbo Frames and Turbo Stream templates ended up explaining things well enough that I could get the issue resolved.
I ended up in this PR to re-discover how a Turbo-Frame-based request can update the browser’s URL. In this case that’s
data-turbo-action
, which in practice looks something like:link_to("My Link", my_path(data: { turbo_frame: "tf_calendar", turbo_action: :advance })
-
Season 1, Issue 5: Promotes Regularity
Good Enough is taking a summer pause! We each have travel plans lined up for the summer. We each have at least two children in our homes, begging for attention. And we each truly appreciate extended breaks as a means of reinvigorating ourselves to the ever important task of building bemusing things. So while you definitely shouldn’t expect anything from us ever, especially don’t expect any more newsletters over the summer. As the Nintendo Wii would say, why not take a break?
-
Season 1, Issue 4: Issue 4-evah
0. It’s Been a Long Time
Sorry 4 the silence these past weeks. Life happened fast and furious. Barry’s daughters all had marching band, and he chaperoned 4 the parades. His eldest daughter also went and got a boyfriend. Shawn’s been feeling a little under the weather, and his two boys graduated. His sons haven’t gotten a girlfriend or boyfriend yet.
Get used to it as there won’t be a newsletter next week, either. It’s good to keep expectations low.
Now on to 4 things 4 this 4th issue.
-
Season 1, Issue 3: Fool Me Three Times
This will be a short one. Barry’s summer vacation has started, and Shawn is deflated after the Warr…iors... sob… It’s just one game, right?
1. No One Likes a Giveaway
Well that didn’t happen. There were no entries to last week’s giveaway. If you recall the rules were (1) tell someone about this newsletter and (b) leave a comment saying as much. As my mother-in-law says to me literally every time I see her, “With friends like these, who needs enemies?” —BH