【发布时间】:2018-05-01 17:11:22
【问题描述】:
我正在尝试链接到自定义控制器路由操作,但我做错了。我有一个 Document 模型,用于处理将文档上传到我的 CRUD 应用程序。我希望用户能够“删除”某些内容,但实际上不能将其从系统中删除,而是将“活动”列更新为 false。然后,如果具有管理员权限的人可以继续完成删除。之所以需要此过程,是因为应用会经过审核,并且我们不想意外删除上传的文件。
我无法使自定义更新操作(删除)起作用。当我搜索路线时,我看到:
remove_documents PUT /documents/remove(.:format) document#remove
在我的路线文件中(我会在稍后添加一些类似的路线,所以我用这种方式收集它):
resources :documents do
collection do
put "remove", to: "document#remove", as: :remove
end
end
在文档索引视图中:
<%= link_to remove_documents_url(document), :method => :put do %>
<span class="fa fa-trash text-danger"></span>
<% end %>
我的控制器:
def remove
@document = Document.find(params[:id])
@document.active = false
@document.save
html { redirect_to(:back, :notice => 'Document was successfully removed.')}
end
该链接有效,但随后出现以下错误:
NameError at /documents/remove.75 uninitialized constant DocumentController
raise unless e.missing_name? qualified_name_for(parent, const_name)
end
end
name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
name_error.set_backtrace(caller.reject {|l| l.starts_with? __FILE__ })
raise name_error
end
# Remove the constants that have been autoloaded, and those that have been
# marked for unloading. Before each constant is removed a callback is sent
【问题讨论】:
-
你能添加整个错误吗?
-
我添加了更多我从更好的错误中看到的内容
-
@BenPohl 控制器名称必须是复数:
put "remove", to: "documents#remove", as: :remove -
谢谢@sovalina。现在我有一个新错误:ActiveRecord::RecordNotFound at /documents/remove.66 找不到带有 'id'= 的文档
-
我不确定如何传递参数 id。我假设在某些 Rails 宁静路线中为您解决了这一问题。
标签: ruby-on-rails controller routing