如何发送FETCH请求以执行Rails destroy_all? [英] How to send FETCH request to perform Rails destroy_all?
问题描述
我想我的API知识仍然很有限,因为我无法弄清楚.
I guess my API knowledge is still limited because I could not figure this out.
我有一个位于React前端和Rails后端的应用程序.如果我想一次删除一个项目,可以执行以下操作:
I have an app that sits on React front end and Rails backend. If I want to delete one item at a time, I can do something like this:
function deleteSomething(somethingId){
return fetch(`api/something/${somethingId}`, {
method: 'DELETE'
})
}
在Rails控制器中,我做类似的事情
and in Rails controller, I do something like
def destroy
@something = Something.find(params[:id]).destroy
head :no_content
end
我的路线如下:
scope :api do
resources :something, only: [:index, :show, :create, :update, :destroy]
...
这次,我想一次销毁多个物品.我发现了这篇 SO帖子,谈论destroy_all;我在Rails控制台上对其进行了测试,它的工作原理就像魔术(Rails本身不是魔术师吗?).
This time, I want to destroy multiple items at once. I found this SO post that talks about destroy_all; I tested it on my rails console and it worked like magic (isn't Rails itself magical?).
但是,我不知道如何编写提取方法或路由.
However, I could not figure out how to write fetch method or the routing.
def destroy
Something.destroy_all(:name => params[:name]) #if I want to destroy all Model with certain name
end
如何编写提取方法?我认为我不应该包含每个ID并一个一个地进行迭代,因为这与为什么我首先要使用destroy_all完全相反.
How can I write the fetch method? I don't think I should include every single ID and iterate one by one, because that goes against why I wanted to use destroy_all in the first place.
function deleteSomehing(name){
return fetch(`api/something/(what goes here?)`, {
method: 'DELETE'
})
}
应该将我的提取方法寻址/路由到哪里?我该如何处理路由,应该在控制器内部为此创建一个新方法,还是可以在destroy内部存根?
Where should my fetch method be addressed/ routed to? What do I do with routing, should I create a new method inside controller for this, or can I stub it inside destroy?
这是我尝试过的方法,但是没有用(错误:Uncaught (in promise) SyntaxError: Unexpected end of JSON input
)
Here is what I have tried, but it did not work (error: Uncaught (in promise) SyntaxError: Unexpected end of JSON input
)
function customDeleteName(name){
return fetch(`api/something/delete_by_name`, {
method: 'POST',
headers: {
'Content-Tye': 'application/json'
},
body: JSON.stringify({
name: name
})
}).then((response) => response.json())
}
内部路由-api作用域:
Inside routes - api scope:
scope :api do
post '/something/delete_by_name' => 'something#delete_by_name'
内部控制器:
def delete_by_name
Something.destroy_all(:name => params[:name])
end
#routes
scope :api do
delete `/something/delete_by_name/:name`, to: 'something#delete_by_name'
#controller
def delete_by_name
Something.destroy_all(:name => params[:name])
head :no_content
end
## JS side ##
#fetch:
function deleteSomething(name){
return fetch(`api/something/delete_by_name/${name}`, {
method: 'DELETE'
})
}
错误:
ActionController::RoutingError (No route matches [DELETE] "/api/something/delete_by_name/somename"):
推荐答案
我想我会在控制器上为其创建特定的方法/操作. 给定模型名称,您可以执行以下操作:
I think I would create a specific method/action for it on the controller. Given the model name, you can do:
"something".capitalize.constantize.destroy_all
如果something
有2个名称,则需要在对其调用常量化之前正确将其大写.
If something
has 2 names you'll need to capitalize it correctly before calling constantize on it.
scope :api do
...
delete 'your/beautiful/route/:name', to: 'wherewild#thingsare'
wherewild_controller.rb
def thingsare
#do magic rails
end
现在,如果您选择与当前删除操作所使用的路径相同的路径,则会遇到问题,因此最好选择另一个路径.
Now, you'll have a problem if you choose the same route path as you're using for the current delete, so better choose another one.
这篇关于如何发送FETCH请求以执行Rails destroy_all?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!