简体   繁体   中英

Rails form_for model not working

I'm trying to create a "gif" object using a form_for. However, when I click submit, I just get redirected to my "gif" index page and nothing is created. I want to hit "submit" and have a "gif" created and be redirected to its page.

Here's my controller page

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

Here's my form on the new page

<%= form_for @gif, :as => :gif, :url => gifs_path do |f| %>
 .....
<% end %>

Terminal Output:

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)

If I change the form_for to :gif instead of @gif, it works. However, I need to use @gif for the gem i'm using, coccoon.

What happens when you remove the :as => :gif, :url => gifs_path from your form like so:

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

It's worth a try, I doubt you need that other stuff

UPDATE:

Your console output shows the following:

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

It also shows the following params being passed:

"gif"=>{
  "title"=>"qwe", 
  "link"=>"http://i.imgur.com/AYPJoxS.gif", 
  "ingredients_attributes"=>{
    "1484430976428"=>{
      "name"=>"123", 
      "_destroy"=>"false"
    }
  }, 
  "directions_attributes"=>{
    "1484430978979"=>{
      "step"=>"123", 
      "_destroy"=>"false"
    }
  }
}

Your gif_params method looks fine, so it seems that something is going wrong when you try to save a gif. To figure out what is happening I would go into rails c and do the following:

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

Above we are passing almost exactly the same params to ActiveRecord, and you will hopefully see some kind of error message on screen when you run this code. It might also be worth running: puts gif.errors to drill down. It's strange you're not getting any error messages when you do this in the browser.

I've seen something similar in one of my own projects where I had configured Pundit incorrectly. Not sure if that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM