简体   繁体   English

使用哪个动作:PUT或POST

[英]Which action to use: PUT or POST

I have resources in routes file: 我在路由文件中有资源:

resources :forwardings

And I need to add the action which will save settings to all items in collection. 我需要添加动作,将设置保存到集合中的所有项目。

The question is which method should I use to implement this one? 问题是我应该使用哪种方法来实现这个方法?

resources :forwardings do
  put 'save_all', :on => :collection
  # or
  post 'save_all', :on => :collection
end

Maybe I'm missing some details and I'd glad to hear out. 也许我错过了一些细节,我很高兴听到。 Thanks 谢谢

You can use both, it doesn't matter, both are 'good' for creating. 你可以同时使用两者,无所谓,两者都是“好”的创造。

Read more here: PUT vs POST in REST 在这里阅读更多内容: 在REST中PUT与POST

Really look into those answers, there are many explanations. 真的看看那些答案,有很多解释。

PUT expects resource-ID in order to perform create/update operation and POST just create resource blindly. PUT需要resource-ID才能执行创建/更新操作,而POST只是盲目地创建资源。 Or we can say that when posting data using POST then resource-ID is assigned by server after performing the requested operation so that using that resource-id further operation can be done. 或者我们可以说当使用POST发布数据时,服务器在执行所请求的操作之后分配资源ID,以便可以使用该资源ID进一步操作。 if we expect resource-id to be allocated from server side then we can use POST or if resource id is to be given by client then we can use PUT. 如果我们期望从服务器端分配resource-id然后我们可以使用POST或者如果客户端给出资源id,那么我们可以使用PUT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM