Posts by Barry Hess
A Good Enough Engineer
More about Barry:
https://bjhess.com
Back to all blog posts
-
· 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):
-
· 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.
-
· 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 })