Agile Web Development with Rails 7: [solved]

AWDWR 7, page 152, page 153:

Hello everyone,

I’m a little bit lost on the hotwire part. I didn’t fully understand it.

On page 152 @rubys does the hotwire in one way. Then on page 153 he creates creatre.turbo_stream.erb and add 2 turbo_stream.replace .... I understand he is showing 2 ways of doing it. However I feel like I got confused. This is a chapter I have to read multiple times.

Can someone explain more about this sentence on page 152: “Since we followed the default
naming conventions for the template, we don’t need to pass any arguments”. I know in rails we use a lot of conventions but this went over my head.

On page 153 I got lost at this line: format.turbo_stream { @current_item = @line_item }, Where did @current_item came from, where do we really use it? We don’t use it at our 2 turbo_stream replace ...

Mert

On page 152 @rubys does the hotwire in one way. Then on page 153 he creates creatre.turbo_stream.erb and add 2 turbo_stream.replace .... I understand he is showing 2 ways of doing it. However I feel like I got confused. This is a chapter I have to read multiple times.

You are correct that I am showing two different ways of constructing a response. I would welcome any suggestions as to how this could be improved.

Can someone explain more about this sentence on page 152: “Since we followed the default naming conventions for the template, we don’t need to pass any arguments”. I know in rails we use a lot of conventions but this went over my head.

What I meant by that is that we don’t need to specify any arguments to render, so in fact no calls to render are required. Compare this code with what it replaced to see what I mean:

          render turbo_stream: turbo_stream.replace(
            :cart,
            partial: 'layouts/cart',
            locals: { cart: @cart }
          )

I’ll rework this paragraph.

On page 153 I got lost at this line: format.turbo_stream { @current_item = @line_item }, Where did @current_item came from, where do we really use it? We don’t use it at our 2 turbo_stream replace ...

It will be used by the rendering of the cart, which is done here:

<%= render partial: 'layouts/cart', locals: {cart: @cart} %>

I’ll add a sentence or two to explain why we are setting this value.

I have to agree with @mert. The part with

format.turbo_stream { @current_item = @line_item }

is a bit confusing, especially since this appears first on these pages, for ‘task F’. When I gripped in the unzipped code examples folder, I only find @current_item in folders ‘depot_n’ (and following).

Here’s what I did to find the occurrences:

~/Downloads/code/rails7 > grep -Rin "@current_item" * | ruby -e "p ARGF.map { |ln| ln.scan(/(depot_.+?)\//) }.flatten.uniq"
["depot_n", "depot_o", "depot_p", "depot_pa", "depot_pb", "depot_pc", "depot_q", "depot_qa", "depot_qb", "depot_r", "depot_s", "depot_t", "depot_ta", "depot_tb", "depot_tc", "depot_u", "depot_ua", "depot_ub", "depot_uc", "depot_xa", "depot_xb", "depot_xc"]

I would have expected this instance variable somewhere in a subfolder of depot_f already.
Am I doing it wrong?

Cheers
Stephan

This is now set on page 156. The very next code fragment uses this value in _line_item.html.erb.

The way to read this code is as follows: whenever we get a request that accepts
a turbo stream response, we render a turbo stream response consisting of
turbo stream replace specifying an HTML element id of cart as the element
to be replaced, and rendering the partial which can be found in
app/views/application/_cart.html.erb using the value of @cart as the value of cart.

Underneath, I would add the partial code:

app/views/application/_cart.html.erb:

<% if cart and not cart.line_items.empty? %>

<%= render cart %>
<% else %>
<% end %>

It took me a couple of hours to realize that I did not have that partial even though I was getting pointed to it.