Не удалось найти товар с 'id' = test - PullRequest
0 голосов
/ 28 мая 2019

В основном работает над настройкой ситуации с любимым продуктом, но я застрял на самом деле, настраивая ее

Это то, что я получил до сих пор

Я думаю, это потому, что я использую 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


View

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

MODEL

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 ]

1 голос
/ 28 мая 2019

здесь ошибка

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) %>
0 голосов
/ 28 мая 2019

Не уверен, что происходит за кулисами с дружественным идентификатором, но вы должны иметь возможность выполнять запрос по названию продукта:

  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

Дело в том, что вы всегда можете использовать имена столбцоввместо того, чтобы полагаться на драгоценный камень.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...