首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails Hotwire + Admin Namespace

Rails Hotwire + Admin Namespace
EN

Stack Overflow用户
提问于 2022-01-04 03:31:34
回答 1查看 213关注 0票数 0

我有一个运行rails 6.1.3.2的应用程序。我开始迁移到使用hotwire而不是ujs。我的应用程序使用一个管理名称空间来允许用户进行编辑,通过这个创建项目。下面的路线-

代码语言:javascript
复制
resources :event_attachments
namespace :admin do
  resources :event_attachments
end

当通过管理视图对event_attachment进行更新时,turbo_stream不是在处理管理视图中的部分,而是在查看EventAttachmentsController。我不知道它为什么要这么做-任何建议都会很感激。

管理员:EventAttachmentsController-

代码语言:javascript
复制
  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

这是我在admin/event_attachments上的表格样本-

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

Rails服务器日志(作为测试,我在主event_attachments视图中创建了一个部分)-

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-02-22 16:34:07

这里的问题似乎是表单发送请求到的url中的问题。如果您查看日志,您将看到它说是EventAttachmentsController#update处理,这是因为在您的形式中,URL是错误的。我的想法是,您应该从使用form_for和[]改为使用form_with

代码语言:javascript
复制
<%= form_with(url: admin_event_attachments_path, :id =>  dom_id(event_attachment)) do |f| %>
  <%= f.text_field :title, :placeholder => "Event image name *" %>
<% end %>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70573814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档