gpt4 book ai didi

javascript - Rails:ActionView::Template::Error while removing row from table using designated .js.erb

转载 作者:数据小太阳 更新时间:2023-10-29 08:35:26 24 4
gpt4 key购买 nike

需要帮助在 Rails 4.2 中使用 jQuery 从表中删除行。单击绿色按钮后,我想删除整行并刷新表格。

至于逻辑,我正在处理文档和用户。用户能够隐藏文档并将注释附加到特定的隐藏。因此,我决定创建额外的模型 Hiding,其中包含对 UserDocument 的引用以及其他相关信息:

隐藏.rb

class Hiding < ActiveRecord::Base
belongs_to :document
belongs_to :user
end

从另一个 Angular 来看,DocumentUser模型有如下关联:

has_one :hiding

单击绿色按钮后,将调用 Controller 的取消隐藏 操作:

class Admin::HidingsController < AdminController
authorize_resource

def index
@hidings = Hiding.all.page params[:page]
end

def unhide
@hidings = Hiding.all.page params[:page]
@hiding = Hiding.where(document_id: params[:id])

# update document and delete hiding from DB
search_un_hide_document(false, nil)
end
end

但是,当我想通过unhide.js.erb

删除表行时
$('#<%=@hiding.document.id%>').remove()

触发了以下异常:

ActionView::Template::Error (undefined method `document' for #Hiding::ActiveRecord_Relation:0x0000000c4508d8>)

由于我是 Rails 的新手,我已经坚持了一段时间。但是,无法弄清楚如何在不重新呈现整个页面的情况下正确删除这些行。

非常感谢您的建议。提前致谢!

最佳答案

def unhide
@hidings = Hiding.all.page params[:page]
@hiding = Hiding.where(document_id: params[:id]).first

# update document and delete hiding from DB
search_un_hide_document(false, nil)
end

Probably you forgot to add .first for @hiding

编辑:

Instead of where().first, you can also use find_by(document_id: params[:id])

find_by 是这样定义的

def find_by(*args)
where(*args).take
end

现在,“take”在顺序上不同于“first”。 first按照主键顺序返回第一条记录,take返回数据库最先吐出的记录。

关于javascript - Rails:ActionView::Template::Error while removing row from table using designated .js.erb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49510950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com