我做了
rails g migration add_position_to_products position:integer
让我解释一下我在这里做的事情的逻辑。为了让我的系统知道将项目放入什么顺序,我需要一个参考框架,数据库中的一些属性可以存储在我可以说的地方,你在这里看到这个位置,这个数字是我的顺序希望你根据它进行排序。
下面我提供了一些视觉效果:
因此,如果我想将这些萝卜射击项目的位置编号设置为5,那么我希望萝卜射击位于下方图像中的位置5,因此现在位于下方5个区域。所以我希望这个系统工作的方式是当我点击并拖动项目并将其移动到不同的位置时,我想要的是让系统去动态地改变那个位置值。
因此上面的rails迁移是我在数据库中为该位置创建元素。
我做了
rails db:migrate
然后我需要创建一个范围。我去了ProductsController并将其放在索引操作中:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
layout 'product'
access all: [:show, :index], user: {except: [:destroy, :new, :create, :update, :edit]}, site_admin: :all
# GET /products
# GET /products.json
def index
@products = Product.order("position ASC")
@products = Product.page(params[:page]).per(9)
@page_title = "Products"
end
# GET /products/1
# GET /products/1.json
def show
@page_title = @product.summary
@seo_keywords = @product.description
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
format.html { redirect_to @product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: @product }
else
format.html { render :edit }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:summary, :description, :image, :user)
end
end
那么索引操作中的上述内容应该是在Products数据库表中查找position属性,并从最低位置开始排序元素并将其调到最高位置。
到目前为止一切顺利。
然而,当我进入我的Rails控制台时,我去了Product.last我得到一个零位置:
Product Load (0.1ms) SELECT "products".* FROM "products" ORDER BY "products"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Product id: 6, summary: "one more", description: "one more product", image: "abstract-1851074_1280.jpg", created_at: "2017-09-12 19:59:13", updated_at: "2017-09-12 19:59:13", user_id: nil, active: nil, price: nil, position: nil>
当我这样做时:
2.3.3 :002 > Product.last.update!(position: 1)
我收到此错误:
NoMethodError: undefined method `name' for #<Product:0x007ff4e7aa82a8>
我不知道name
的未定义方法在哪里,因为我们之前已将Product.summary更改为Product.name,但我发现它破坏了它。所以我把它改回了Product.summary。那么name
的未定义方法是指什么?
这是schema.rb文件:
ActiveRecord::Schema.define(version: 20170915143515) do
create_table "carts", force: :cascade do |t|
t.integer "user_id"
t.integer "product_id"
t.integer "quantity"
t.float "subtotal"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["product_id"], name: "index_carts_on_product_id"
t.index ["user_id"], name: "index_carts_on_user_id"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "homes", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "products", force: :cascade do |t|
t.string "summary"
t.text "description"
t.string "image"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.boolean "active"
t.float "price"
t.integer "position"
t.index ["user_id"], name: "index_products_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "name"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "roles"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
end
这是models / product.rb文件:
class Product < ApplicationRecord
belongs_to :user
validates :user , presence: true
mount_uploader :image, ImageUploader
has_and_belongs_to_many :categories
validates :summary, :price, presence: true
has_and_belongs_to_many :categories
end
在控制台中我看到有一个user_id:和price:这是nil,并且如上所述,它不能为零:true。
我没有开发出那些功能,看起来就像我开发编辑表单后完成的那样。此时,我不知道如何在Stack Overflow上继续。错误/问题现在已经改变,请指教。我想我可以将Jacobs的答案标记为正确答案,因为通过提及Products模型,它可以让我弄清楚为什么我会得到未定义的名称方法。
答案 0 :(得分:0)
您可以发布您的产品型号吗?
我很高兴我的问题导致你找到了解决方案!为了防止以后有人碰到这个页面,我将尝试总结@Ale为自己找到的解决方案......
origional product.rb:
class Product < ApplicationRecord
belongs_to :user
validates :user , presence: true
mount_uploader :image, ImageUploader
has_and_belongs_to_many :categories
validates :name, :price, presence: true
has_and_belongs_to_many :categories
end
更新了product.rb:
class Product < ApplicationRecord
belongs_to :user
validates :user , presence: true
mount_uploader :image, ImageUploader
has_and_belongs_to_many :categories
validates :summary, :price, presence: true
has_and_belongs_to_many :categories
end