简体   繁体   English

Rails通过GET创建数据库条目?

[英]Rails create database entry by GET?

is it possible to create a database entry (create method) by a GET request instead of POST? 是否可以通过GET请求而不是POST来创建数据库条目(创建方法)?

thanks in advance. 提前致谢。

Now I have some more details, here is some answer. 现在我有更多细节,这里有一些答案。 If you'll receive this kind of request: 如果您收到此类请求:

your_route?price=123&name=abc

Make a route like: 制作一条路线:

match 'your_route/:price/:name' => 'your_controller#your_method'

If ever you've optional params, use brackets. 如果您有可选的参数,请使用括号。

in your controller, you'll get the params. 在您的控制器中,您将获得参数。 Given you have a Product model with columns price and name , proceed as follows to save it: 如果您有具有列pricenameProduct型号,请按以下步骤进行保存:

@product = Product.new({:price => params[:price]), :name => params[:name]})
@product.save

What you do in response to a GET request is entirely up to you. 您对GET请求的回应完全取决于您。 Of course, multiple GET requests with the same parameters could potentially create multiple records, which might not be what you want. 当然,具有相同参数的多个GET请求可能会创建多个记录,这可能不是您想要的。 Browsers will resend GET requests but will normally prompt the user to resent POST requests. 浏览器将重新发送GET请求,但通常会提示用户重新发送POST请求。

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

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