【问题标题】:Rails form_for model not workingRails form_for模型不起作用
【发布时间】:2017-01-14 21:29:38
【问题描述】:

我正在尝试使用 form_for 创建一个“gif”对象。但是,当我单击提交时,我只是被重定向到我的“gif”索引页面并且没有创建任何内容。我想点击“提交”并创建一个“gif”并将其重定向到其页面。

这是我的控制器页面

class GifsController < ApplicationController

def index
    if params[:tag]
        @gifs = Gif.tagged_with(params[:tag])
    elsif params[:search]
        @gifs = Gif.search(params[:search])
    else
        @gifs = Gif.all
    end
end

def show
    @gif = Gif.find(params[:id])
end

def new
    @gif = Gif.new
end

def create
    @gif = Gif.new(gif_params)

    @gif.save
    redirect_to @gif
end

private
def gif_params
    params.require(:gif).permit(:title, :link, :recipe, :all_tags, ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy])
end

end

这是我在新页面上的表单

<%= 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)

如果我将 form_for 更改为 :gif 而不是 @gif,它会起作用。但是,我需要将 @gif 用于我正在使用的 gem,coccoon。

【问题讨论】:

    标签: ruby-on-rails forms


    【解决方案1】:

    当您像这样从表单中删除 :as =&gt; :gif, :url =&gt; 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。不确定这是否有帮助。

    【讨论】:

    • 试过了,还是一样
    • 嗯,我想是时候看看当你点击提交按钮时发生了什么样的请求。你可以发布你的终端输出吗?我可能应该先问这个:)
    • puts gif.errors 时没有错误。控制台输出:=> #i.imgur.com/AYPJoxS.gif", created_at: nil, updated_at: nil, tags: nil, recipe: nil>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多