Ruby on Rails:提交的表单不会插入到 SQL 中
你好 我的应用程序遇到了一个奇怪的问题,它不会保存到 sqlite 数据库。当我查看日志时,它给了我这个。
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/+...+...=", "product"=>{"title"=>"test", "user_id"=>"1"}, "commit"=>"Create Product"}
Product Load (0.2ms) SELECT "products"."id" FROM "products" WHERE ("products"."title" IS NULL) LIMIT 1
AREL (0.3ms) INSERT INTO "products" ("title", "body", "created_at", "updated_at", "user_id") VALUES (NULL, NULL, '2011-03-21 09:59:57.546656', '2011-03-21 09:59:57.546656', NULL)
这可能是一个新手错误,但我不知道该怎么办:/
Hi
I've come across a weird problem with my application, It wont save to the sqlite DB. When I look at the log it gives me this.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/+...+...=", "product"=>{"title"=>"test", "user_id"=>"1"}, "commit"=>"Create Product"}
Product Load (0.2ms) SELECT "products"."id" FROM "products" WHERE ("products"."title" IS NULL) LIMIT 1
AREL (0.3ms) INSERT INTO "products" ("title", "body", "created_at", "updated_at", "user_id") VALUES (NULL, NULL, '2011-03-21 09:59:57.546656', '2011-03-21 09:59:57.546656', NULL)
This is problaby a newbiefault, but I don't know what to do :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建对象时,您需要将 params 哈希传递给对象,如下所示:
You need to pass the params hash to your object when you create it like so:
它似乎试图为您创建的所有字段插入 NULL 值。
问题一定出在控制器文件上,而不是sql代码上。
您可能正在保存一个没有设置任何属性的对象。
您是使用 new 还是使用 create 创建对象?因为如果我没记错的话,create 会采用参数的哈希值并立即将对象保存到数据库中。
也就是说,如果你去 Myclass.create(),它将失败。
当然,如果粘贴控制器代码,调试起来会更容易。
另外,您是否从 params['product'] 获取 product 参数?
即 params['product']['title'] 或 params['title']?
It seems to be trying to insert NULL values for all the fields you've created.
The problem must be in the controller file, not the sql code.
You're probably saving an object that doesn't have any properties set.
Are you creating your object with new or with create? Because if I remmeber correctly create takes a hash of parameters and immediately saves the object to the database.
I.e. if you go Myclass.create(), it will fail.
Of course, it would be easier to debug this if you paste your controller code.
Also, are you getting the product parameters from params['product'] ?
I.e. params['product']['title'] or params['title']?