2017-09-15 11 views
-1

私は`name`の定義されていないメソッドはどこから来ますか?

rails g migration add_position_to_products position:integer 

は私が私がここでやっている何のロジックを説明しましょうでした。私のシステムがアイテムを入れる順序を知るために、私は参照の枠組みが必要です。データベース内のいくつかの属性は、ここで私が言うことができる場所に格納することができます。あなたはソートの基礎を築きたいと思っています。 I以下

の視覚のビットを提供します。

enter image description here

私はこれらの大根は5にアイテムを撮影するための位置番号を設定したいのであれば、私は大根の芽が位置5にあると期待されます下の画像にあるので、今のところ5ブロック下にあります。そして、私はこのシステムを動作させたいのですが、アイテムをクリックしてドラッグして別の位置に移動すると、システムが移動してその位置の値を動的に変更する必要があります。

したがって、上記のレールの移動は、私の位置でデータベースに要素を作成しています。

私はその後、私はスコープを作成するために必要な

rails db:migrate 

をしました。私はProductsControllerに行って、indexアクションでそれを置く:

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 

そうすべきindexアクションで上記の製品のデータベーステーブルの位置属性を探して、最低で始まり行くの要素を注文何でありますか最高に。

これまでのところとても良いです。

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) 

私はこのエラーを取得する:私は私のRailsコンソールに行くとき

しかし、私は私がnilの位置を取得しProduct.lastを行く

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 

これは、モデル/ 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

コンソールで 'reload!'をやったことがありますか? –

+0

@RajMishra、リロード・バンを行った後でも、私はまだエラーが発生します。 – Ale

答えて

1

Can you post your Product model?

解決策を見つけることができて嬉しく思います!

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 

更新された製品:ちょうど誰かが後でこのページに出くわす場合には、私は自分自身が見つかり@Aleソリューション...

origional product.rbを総括しようとします。 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元のファイルの内容が大きく間違っている場合はお知らせください。更新します。 –

関連する問題