I truly am trying to get my head around this conceptually. Every time I think I know what I’m doing with Rails I hit a wall (very hard) and then feel rather stupid. I’m trying to understand Turbo Frames. I thought that these were basically sections of the webpage which could be updated independently from the rest of the page. A button click would lead to the server sending some “updated” code which turbo would see the frame and update it accordingly. This is what I thought I was doing … but apparently I’ve missed something.
I have a show.html.erb
page with a turbo frame in it. It shows an image of a hamper.
<div class="w-full">
<%= turbo_frame_tag "hamperimage" do %>
<%= render partial: 'hamper_image', locals: {hamperphoto: @hamperphoto} %>
<% end %>
</div>
Later in the same page I have a list of options which trigger a change of the picture (and other parts of the page but I’m trying to get this to work first!). This used to work with a link_to, a remote:true and some javascript. I have removed the “remote: true” and changed it to a button_to (following a tutorial which said to do this!). I’ve removed the javascript. Here is the code which should action a change:
<% @hamper.hamper_options.order(option_id: :asc).each do |hamperoption| %>
<%= button_to change_option_hamper_path(option: hamperoption.option.name), method: :post do %>
<% if hamperoption&.image_url(:small) %>
<%= image_tag hamperoption.image_url(:small), class: "w-24 mr-5" %>
<% else %>
<%= image_tag "processingimage.png", class: "w-24" %>
<% end %>
<% end %>
<% end %>
When I click the image expecting a change, it triggers the action:
def change_option
@hamper = Hamper.friendly.find(params[:id])
@option = Product.where(name: params[:option]).first
@option ||= ''
cookies.signed[:htviewing] = JSON.generate({ data: { hamper: @hamper.id, option: @option.id.to_s } })
@saleprice = @hamper.saleprice + @option.sale_price
@optionid = @option.id
@hamperphoto = HamperOption.where(hamper: @hamper, option: @option).first
render :layout => false
end
Which renders the change_option.html.erb file:
<%= turbo_frame_tag "hamperimage" do %>
<%= render partial: 'hamper_image', locals: {hamperphoto: @hamperphoto} %>
<% end %>
Which … when I look at the network response delivers the following:
<turbo-frame id="hamperimage">
<img class="object-contain w-full px-6" src="https://hampertree.nyc3.digitaloceanspaces.com/78609b009426d0845d1c0efd2b23af37.jpg" />
</turbo-frame>
So … as far as I understand things … it should now replace the turbo-frame with the new content? Is that correct? I am not using turbo streams (yet) … as I thought it was the case that Turbo would look for the turbo frame and replace it automatically.
The status code is coming back as 200 OK.
However, nothing is changing on the page.
I am obviously doing something really stupid - but can’t work out where my conceptual understanding is going wrong.
Thanks in advance
PS. Please break it down into basics!