我试图创建一个" gif"使用form_for的对象。但是,当我点击提交时,我只是被重定向到我的" gif"索引页面,没有创建任何内容。我想点击"提交"有一个" gif"创建并重定向到其页面。
这是我的控制器页面
<%= form_for @gif, :as => :gif, :url => gifs_path do |f| %>
.....
<% end %>
这是我在新页面上的表单
Started POST "/gifs" for 69.127.215.48 at 2017-01-14 21:56:18 +0000
Processing by GifsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"cPycHb5KywsTgLbHN+sZJjSbyAbGlGce4GDpqolTA5vQNCScBszU1C0wXqFF+jB5Y6OYfSSK0PTe1Qod9O1aGA==", "gif"=>{"title"=>"qwe", "link"=>"http://i.imgur.com/AYPJoxS.gif", "ingredients_attributes"=>{"1484430976428"=>{"name"=>"123", "_destroy"=>"false"}}, "directions_attributes"=>{"1484430978979"=>{"step"=>"123", "_destroy"=>"false"}}}, "commit"=>"Upload"}
(0.2ms) begin transaction
(0.1ms) rollback transaction
Redirected to https://recipes-in-a-gif-dleggio1.c9users.io/gifs
Completed 302 Found in 196ms (ActiveRecord: 0.3ms)
Started GET "/gifs" for 69.127.215.48 at 2017-01-14 21:56:18 +0000
Processing by GifsController#index as HTML
Rendering gifs/index.html.erb within layouts/application
Gif Load (0.3ms) SELECT "gifs".* FROM "gifs"
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."gif_id" = ? [["gif_id", 1]]
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."gif_id" = ? [["gif_id", 2]]
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."gif_id" = ? [["gif_id", 3]]
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."gif_id" = ? [["gif_id", 4]]
Tag Load (0.1ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."gif_id" = ? [["gif_id", 5]]
Rendered gifs/index.html.erb within layouts/application (11.2ms)
Completed 200 OK in 73ms (Views: 70.6ms | ActiveRecord: 0.8ms)
终端输出:
{{1}}
如果我将form_for更改为:gif而不是@gif,则可以正常工作。但是,我需要使用@gif作为使用coccoon的宝石。
答案 0 :(得分:1)
当您从表单中删除:as => :gif, :url => gifs_path
时会发生什么情况:
<%= form_for @gif do |f| %>
.....
<% end %>
值得一试,我怀疑你需要其他东西
更新:
您的控制台输出显示以下内容:
(0.2ms) begin transaction
(0.1ms) rollback transaction
它还显示以下参数:
"gif"=>{
"title"=>"qwe",
"link"=>"http://i.imgur.com/AYPJoxS.gif",
"ingredients_attributes"=>{
"1484430976428"=>{
"name"=>"123",
"_destroy"=>"false"
}
},
"directions_attributes"=>{
"1484430978979"=>{
"step"=>"123",
"_destroy"=>"false"
}
}
}
您的 gif_params 方法看起来很好,所以当您尝试保存gif时似乎出现了问题。为了弄清楚发生了什么,我会进入 rails c 并执行以下操作:
gif = Gif.create "title"=>"qwe", "link"=>"http://i.imgur.com/AYPJoxS.gif", "ingredients_attributes"=>{ "1484430976428"=>{ "name"=>"123", "_destroy"=>"false" }}, "directions_attributes"=>{ "1484430978979"=>{ "step"=>"123", "_destroy"=>"false" }}
上面我们将几乎完全相同的参数传递给ActiveRecord,当您运行此代码时,您希望在屏幕上看到某种错误消息。也许值得运行:puts gif.errors
向下钻取。在浏览器中执行此操作时,您没有收到任何错误消息,这很奇怪。
我在我自己的一个项目中看到了类似的东西,我在那里错误地配置了Pundit。不确定这是否有帮助。