为什么在保存重复的Ruby on Rails模型时出现500错误(未定义的方法"to_model" [英] Why do I get a 500 error (undefined method 'to_model' on save of duplicated Ruby on Rails Model
问题描述
我是Rails的新手,在某些事情上仍然很模糊,但这是可行的.
I'm new to rails and still fuzzy on some things but here goes.
我正在向/microposts/10/duplicate.json发送json发布请求,以复制一个微博.
I am sending json post request to /microposts/10/duplicate.json to duplicate a micropost.
路线转到微帖#duplicate
The route goes to microposts#duplicate
Controller动作是:
The Controller action is:
def duplicate
micropost = current_user.microposts.find(params[:id])
new_micropost = micropost.dup
respond_with new_micropost.save
end
该请求的服务器日志为:
The server logs for the request is:
Started POST "/microposts/15/duplicate.json" for 180.181.247.76 at 2015-09-06 11:00:38 +0000
Processing by MicropostsController#duplicate as JSON
Parameters: {"_json"=>15, "id"=>"15", "micropost"=>{}}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
Micropost Load (0.3ms) SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = ? AND "microposts"."id" = ? ORDER BY "microposts"."created_at" DESC LIMIT 1 [["user_id", 2], ["id", 15]]
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "microposts" ("content", "user_id", "title", "link", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["content", "RLCPYIIG"], ["user_id", 2], ["title", "herp"], ["link", "snerp"], ["created_at", "2015-09-06 11:00:38.576553"], ["updated_at", "2015-09-06 11:00:38.576553"]]
(11.7ms) commit transaction
Completed 500 Internal Server Error in 28ms (ActiveRecord: 12.8ms)
NoMethodError (undefined method `to_model' for true:TrueClass):
app/controllers/microposts_controller.rb:23:in `duplicate'
因此微博已保存,但我不知道为什么会收到500错误. 另外,我应该在这里使用.require和.permit吗?前端参数中唯一的东西应该是id.
So the micropost is saved but I don't know why I get the 500 error. Also, should I be using .require and .permit here? The only thing in params from the front end should be the id.
任何帮助将不胜感激.
推荐答案
我最近在使用respond_with
时遇到了类似的错误.
I was recently getting a similar error when using respond_with
.
原来,我只是在请求HTML响应时缺少动作的相应视图.
Turned out I was just missing the corresponding view for my action when requesting a HTML response.
不幸的是,该错误是非常隐秘的.
Unfortunately the error was rather cryptic with..
undefined method `to_model' for #<Array:0x007fc7c59ada50> Did you mean? to_xml
TLDR: 检查您是否有相应的视图.
希望这对某人有帮助.
这篇关于为什么在保存重复的Ruby on Rails模型时出现500错误(未定义的方法"to_model"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!