我正在尝试链接到自定义控制器路由操作,我做错了。我有一个Document模型,用于处理将文档上传到我的CRUD应用程序。我希望用户能够“删除”某些内容,但实际上并未将其从系统中删除,而是将“active”列更新为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
答案 0 :(得分:2)
如果您希望对特定<div class="wrapper">
<div class="content">
<p>Lorem ipsum dolor sit amet, at vix dicunt aliquam dignissim. Bonorum assentior liberavisse mei et. Recusabo moderatius in pri, te dicam contentiones mei, eam dicta inermis dissentiet eu. Usu ne malis minim reprimique, aeque audire sadipscing cu eos, et nihil latine qui.Lorem ipsum dolor sit amet, at vix dicunt aliquam dignissim. Bonorum assentior liberavisse mei et. Recusabo moderatius in pri</p>
<span class="tit">Move this span outside</span>
</div>
</div>
执行remove
操作,请将路线更改为:
Document
为您提供:resources :documents do
member do
put "remove", to: "documents#remove", as: :remove
end
end
并使用它:
remove_document PUT /documents/:id/remove(.:format)