0

I have an app running rails 6.1.3.2. I'm starting to migrate to use hotwire instead of ujs. My app uses an admin namespace to allow a user to make edits, create items via this. Example route below -

resources :event_attachments
namespace :admin do
  resources :event_attachments
end

When making an update via the admin view to an event_attachment, the turbo_stream is not processing the partial in the admin view, instead it's looking in EventAttachmentsController. I'm not sure why it's doing this - any suggestions would be appreciated.

Admin:EventAttachmentsController -

  def update
    if @event_attachment.update(event_attachment_params)
      respond_to do |format|
        format.turbo_stream do
          render turbo_stream: [
            turbo_stream.update("flash", partial: "shared/flash", locals: { notice: "Event image updated" }),
            turbo_stream.replace(:event_attachments, partial: 'admin/pages/event_attachment', locals: { event_attachment: @event_attachment })
          ]
        end

        format.html do
          redirect_to edit_admin_event_attachment_path(@event_attachment), notice: 'Event image updated'
        end
      end
    else
      render :edit, status: :unprocessable_entity
    end
  end

Here's a sample of my form in admin/event_attachments -

<%= form_for([:admin, event_attachment], :id =>  dom_id(event_attachment)) do |f| %>
  <%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>

Rails Server Log (as a test, I created a partial in the main event_attachments view) -

Started PATCH "/event_attachments/25" for ::1 at 2022-01-03 22:18:53 -0500
Processing by EventAttachmentsController#update as TURBO_STREAM
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "event_attachment"=>{"title"=>"Test Name", "summary"=>"caption"}, "commit"=>"Save Changes", "id"=>"25"}
  EventAttachment Load (1.1ms)  SELECT `event_attachments`.* FROM `event_attachments` WHERE `event_attachments`.`id` = 25 LIMIT 1
  ↳ app/controllers/event_attachments_controller.rb:51:in `set_event_attachment'
  Rendered event_attachments/_event_attachment.html.erb (Duration: 0.1ms | Allocations: 22)
[ActionCable] Broadcasting to event_attachments: "<turbo-stream action=\"update\" target=\"event_attachment_25\"><template><li id=\"event_attachment_25\">\n  hello\n</li>\n</template></turbo-stream>"
Redirected to http://localhost:3000/event_attachments/25
Completed 302 Found in 4ms (ActiveRecord: 1.1ms | Allocations: 1640)

1 Answers1

0

It looks like the problem here is in the url that your form is sending the request to. If you look in the logs you will see it says processing by EventAttachmentsController#update this is because in your form the URL is wrong. My thoughts is you should change from using form_for and the [] to use form_with

<%= form_with(url: admin_event_attachments_path, :id =>  dom_id(event_attachment)) do |f| %>
  <%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>
yungindigo
  • 241
  • 1
  • 6
  • Might be a naive response but doesn't `form_for([:admin, event_attachment]` do the samething as specifying the url? I believe I've tried this and was not seeing it work. Alternatively, specifying the partial location in the model seems to work. – Arash Hadipanah Apr 11 '22 at 19:47
  • It should work for namespacing like that but since its not you could check the html from the developer console on the browser and see what the generated `
    ` tags action is. the `url:` option is a way to force the form to use the URL you want
    – yungindigo Apr 12 '22 at 17:16