samedi 9 mai 2015

Rails 4: sometimes records are saved and sometimes they are not?

I have a weird thing happening. I have a Model call Recipe that has_many :informations and :directions. When I try to save the @recipe (see my controller) with its many :informations and :directions; sometimes it works and sometimes it does not without me changing anything to the source code.

Also, the :informations are always saved; the problem seems to only be regarding the :directions

Here is my controller:

    def new
        @recipe = current_user.recipes.new
    end

    def create
        @recipe = current_user.recipes.new(recipe_params)

        if @recipe.save
            redirect_to @recipe, notice: "Successfully created new recipe"
        else
            render 'new'
        end
    end     
    private

    def recipe_params
        params.require(:recipe).permit(:category_id, :title, :description, informations_attributes: [:id, :title, :url, :_destroy], directions_attributes: [:id, :title, :url, :step, :_destroy])
    end
end

When I try to save from my form here are the params:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"wfa+wTI+C3hnvscAC1O922EYlssJE3zAbEmmqMbmR+5krb0b17fkWhsXVAW1aFcom8x11uGqGO6drQudGhdcvA==", "recipe"=>{"title"=>"test", "description"=>"test", "category_id"=>"3", "informations_attributes"=>{"1431197959831"=>{"title"=>"test", "url"=>"dede", "_destroy"=>"false"}, "1431197959835"=>{"title"=>"ded", "url"=>"dede", "_destroy"=>"false"}}, "directions_attributes"=>{"1431197963709"=>{"title"=>"dede", "url"=>"dede", "step"=>"de", "_destroy"=>"false"}, "1431197963712"=>{"title"=>"ded", "url"=>"ded", "step"=>"ded", "_destroy"=>"false"}}}, "commit"=>"Create Theme"}

Shema:

  create_table "directions", force: :cascade do |t|
    t.text     "step"
    t.integer  "recipe_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "url"
    t.string   "title"
  end


  create_table "information", force: :cascade do |t|
    t.string   "url"
    t.integer  "recipe_id"
    t.datetime "created_at",                            null: false
    t.datetime "updated_at",                            null: false
    t.integer  "cached_votes_total",      default: 0
    t.integer  "cached_votes_score",      default: 0
    t.integer  "cached_votes_up",         default: 0
    t.integer  "cached_votes_down",       default: 0
    t.integer  "cached_weighted_score",   default: 0
    t.integer  "cached_weighted_total",   default: 0
    t.float    "cached_weighted_average", default: 0.0
    t.string   "title"
  end


  create_table "recipes", force: :cascade do |t|
    t.string   "title"
    t.text     "description"
    t.integer  "user_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "category_id"
  end

Thanks for your help !

Aucun commentaire:

Enregistrer un commentaire