2017-09-15 38 views
-1

我做了`name`的未定义方法来自哪里?

rails g migration add_position_to_products position:integer 

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

下面我提供一点视觉的:

enter image description here

所以,如果我想设置的位置编号,这些萝卜芽项目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 

那么上述索引行动应该做的是寻找在产品数据库表中的位置属性,并责令元素从最低和去到最高。

到目前为止这么好。

然而,当我进入我的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这个undefined方法是指什么?

这里是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 

这是模型/ 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:和价格:那是零,并且不能像你在上面看到的那样是零:是的。

我没有开发出这些功能,看起来他们是在我开发编辑表单后完成的。在这一点上,我不知道如何在这里进行堆栈溢出。错误/问题现在已经改变,请告知。我想我可以将雅各布的答案标记为正确的答案,因为通过提及产品模型,它让我找出为什么我得到未定义的名称方法。

+0

我没有看到在任何地方都使用了Product#name。这段代码现在是否会提出这个错误? –

+0

你是否已经在控制台上重新加载了! –

+0

@RajMishra,即使我重新加载后,我仍然得到错误。 – Ale

回答

1

Can you post your Product model?

我很高兴我的问题导致您找到解决方案!万一有人遇到这个页面后,我会尝试去总结这@Ale发现为自己的解决方案...

原始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 

更新的产品。 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 
+0

@Ale ,如果我对原始文件内容有重大错误,请告诉我,我会更新。 –