代码之家  ›  专栏  ›  技术社区  ›  user8359832

“name”的未定义方法来自哪里?

  •  -2
  • user8359832  · 技术社区  · 7 年前

    rails g migration add_position_to_products position:integer
    

    让我解释一下我在这里做什么的逻辑。为了让我的系统知道项目的顺序,我需要一个参考框架,数据库中的一些属性,可以存储在我可以说的地方,你看这个位置,这个数字是我希望你排序的顺序。

    下面我提供了一点视觉效果:

    enter image description here

    因此,如果我想将这些萝卜芽项目的位置号设置为5,那么我希望萝卜芽位于下面图像中的位置5,即现在的位置下方的5个块。因此,我希望这个系统工作的方式是当我点击并拖动项目并将其移动到另一个位置时,我想要的是让系统运行并动态更改该位置值。

    我做了一个

    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属性,并对元素进行排序,从最低到最高。

    到现在为止,一直都还不错。

    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 是因为我们换了产品。产品摘要。以前的名字,但我发现它打破了东西。所以我把它改回产品。总结那么什么未定义的方法呢 它是指?

    这是模式。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
    

    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\u id:and price:that is nil,that not be nil,正如您在上面看到的,presence:true。

    我没有开发这些功能,看起来它们是在我开发编辑表单之后完成的。此时,我不确定如何处理堆栈溢出。错误/问题现已更改,请告知。我想我可以把雅各布的答案标记为正确的答案,因为通过提及产品模型,它让我明白了为什么我得到了未定义的名称方法。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Jacob Vanus    7 年前

    我很高兴我的问题让你找到了解决办法!为了以防万一以后有人看到这一页,我将尝试总结一下@Ale为自己找到的解决方案。。。

    原产产品。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
    

    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