2017-01-14 8 views
1

私はform_forを使って "gif"オブジェクトを作成しようとしています。しかし、私がsubmitをクリックすると、私は "gif"インデックスページにリダイレクトされ、何も作成されません。私は "提出"をヒットし、 "gif"を作成してそのページにリダイレクトしたいと思います。私はのform_forを変更した場合Rails form_for model not working

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) 

ここに私のコントローラページは

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

ターミナル出力ですgifの代わりに@ gif、それは動作します。しかし、私は使用している宝石、コククーンのために@gifを使用する必要があります。あなたはそのようなあなたのフォームから:as => :gif, :url => gifs_pathを削除するとどうなり

答えて

1

あなたのコンソール出力を示し:

<%= form_for @gif do |f| %> 
..... 
<% end %> 

それは試してみる価値がある、私はあなたが他のものを必要とし

UPDATEを疑います以下:

(0.2ms) begin transaction 
(0.1ms) rollback transaction 

また、渡されPARAMSにより:

"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画像を保存しようとすると、何かが間違って起こっているようです。私たちはActiveRecordのにほとんど同じのparamsを渡している、とあなたがうまくいけば、画面上のエラーメッセージのいくつかの種類が表示されます上

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" }} 

:何が起こっているかを把握するために、私はレールCに行くだろうし、次の操作を行いこのコードを実行するとドリルダウンするには、puts gif.errorsを実行する価値があります。ブラウザでこれを行うときにエラーメッセージが表示されないのは不思議です。

Punditを間違って設定した自分のプロジェクトの1つで、何か似たようなことがあります。それが役立つかどうかはわかりません。

+0

まだ同じことを試してみた – user3182252

+0

うーん、送信ボタンを押したときにどのようなリクエストが行われているかを調べるときだと思う。端末の出力を投稿できますか?私はおそらく最初に尋ねたはずです:) – stephenmurdoch

+0

元の投稿に感謝、 – user3182252