找不到带有'id'= test的产品

时间:2019-05-28 12:25:08

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

基本上是在设置喜欢的产品的情况下工作,但我一直坚持实际设置

这是我到目前为止所得到的

我认为这是因为我使用的是友好网址-我添加了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(参数...

  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

Schema.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

当前错误 enter image description here

2 个答案:

答案 0 :(得分:1)

这是错误所在

pip install appjar

替换为

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

答案 1 :(得分:0)

不确定带有友好ID的幕后情况,但您应该可以通过产品名称进行查询:

  product = Cproduct.find_by(title: params[:cproduct]).take
  if product
    khollection = Khollection.where(cproduct_id: product.id), user: current_user).take
    if khollection.blank?
      # Create the khollection
      Khollection.create(cproduct: product, user: current_user)
      @khollection_exists = true
    else
      # Delete the khollection
      khollection.destory_all
      @khollection_exists = false
    end
  else
    # maybe return an error status "Product not found"
  end

  respond_to do |format|
    format.html {}
    format.js {}
  end

重点是,您始终可以依靠列名,而不必依赖gem。