首页 > 解决方案 > 找不到带有 'id'=test 的产品

问题描述

基本上是在设置一个最喜欢的产品情况,但我坚持实际设置它

这是我到目前为止得到的

我认为这是因为我使用的是 Friendly URL - 我添加了friendly.find .. 但我仍然在那里遇到另一个错误(见第二张图片)

控制器


  def update
    khollection = Khollection.where(cproduct: Cproduct.find(params[:cproduct]), user: current_user)
    if khollection == []
        # Create the khollection
        Khollection.create(cproduct: Cproduct.find(params[:cproduct]), user: current_user)
        @khollection_exists = true
    else
        # Delete the khollection
        khollection.destory_all
        @khollection_exists = false
    end
    respond_to do |format|
        format.html {}
        format.js {}
    end
  end


看法

<%= link_to 'Favorite', khollections_update_path(cproduct: @cproduct.title) %>

下面是友好的 .friendly.find(params...

  def update
    khollection = Khollection.where(cproduct: Cproduct.friendly.find(params[:cproduct]), user: current_user)
    if khollection == []
        # Create the khollection
        Khollection.create(cproduct: Cproduct.friendly.find(params[:cproduct]), user: current_user)

模型

class Khollection < ApplicationRecord
  belongs_to :cproduct
  belongs_to :user
end

架构.rb

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_05_28_112623) do

  create_table "active_storage_attachments", force: :cascade do |t|
    t.string "name", null: false
    t.string "record_type", null: false
    t.integer "record_id", null: false
    t.integer "blob_id", null: false
    t.datetime "created_at", null: false
    t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
    t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
  end

  create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

  create_table "cproducts", force: :cascade do |t|
    t.string "title"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "user_id"
    t.string "slug"
    t.index ["slug"], name: "index_cproducts_on_slug", unique: true
  end

  create_table "friendly_id_slugs", force: :cascade do |t|
    t.string "slug", null: false
    t.integer "sluggable_id", null: false
    t.string "sluggable_type", limit: 50
    t.string "scope"
    t.datetime "created_at"
    t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
    t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
    t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id"
  end

  create_table "khollections", force: :cascade do |t|
    t.integer "Cproduct_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["Cproduct_id"], name: "index_khollections_on_Cproduct_id"
    t.index ["user_id"], name: "index_khollections_on_user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "email"
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.text "full_company_logo"
    t.text "domain"
    t.string "business_name"
    t.string "tags"
    t.text "social_media"
    t.text "our_story"
    t.text "location_address"
    t.string "location_city"
    t.string "location_state"
    t.integer "phone"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "admin"
    t.boolean "company"
    t.boolean "judge"
    t.boolean "blogger"
    t.string "username"
    t.string "slug"
    t.string "avatar"
    t.string "company_logo"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["slug"], name: "index_users_on_slug", unique: true
  end


end

当前错误 在此处输入图像描述

标签: ruby-on-railsrubyruby-on-rails-5

解决方案


这是错误所在

create_table "khollections", force: :cascade do |t|
    t.integer "Cproduct_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["Cproduct_id"], name: "index_khollections_on_Cproduct_id"
    t.index ["user_id"], name: "index_khollections_on_user_id"
  end

用。。。来代替

create_table "khollections", force: :cascade do |t|
    t.integer "cproduct_id"
    t.integer "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["cproduct_id"], name: "index_khollections_on_cproduct_id"
    t.index ["user_id"], name: "index_khollections_on_user_id"
  end

khollection = Khollection.where(cproduct: Cproduct.find_by(title: params[:cproduct]), user: current_user)



Khollection.create(cproduct: Cproduct.find_by(title: params[:cproduct]), user: current_user)


<%= link_to 'Favorite', khollections_update_path(cproduct: @cproduct.title) %>

推荐阅读