Ruby on rails “name”的未定义方法来自哪里?
我做了一个测试Ruby on rails “name”的未定义方法来自哪里?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我做了一个测试 rails g migration add_position_to_products position:integer rails db:migrate 让我解释一下我在这里做什么的逻辑。为了让我的系统知道项目的顺序,我需要一个参考框架,数据库中的一些属性,可以存储在我可以说的地方,你看这个位置,这个数字就是我希望你排序的顺序 下面我提供一点视觉效果: 因此,如果我想将这些萝卜芽项目的位置编号设置为5,那么我希望萝卜芽位于下图中的位置5,即现在所在位置下方的5个块处。因此,
rails g migration add_position_to_products position:integer
rails db:migrate
让我解释一下我在这里做什么的逻辑。为了让我的系统知道项目的顺序,我需要一个参考框架,数据库中的一些属性,可以存储在我可以说的地方,你看这个位置,这个数字就是我希望你排序的顺序
下面我提供一点视觉效果:
因此,如果我想将这些萝卜芽项目的位置编号设置为5,那么我希望萝卜芽位于下图中的位置5,即现在所在位置下方的5个块处。因此,我希望这个系统工作的方式是,当我点击并拖动项目,并将其移动到另一个位置时,我希望的是系统运行并动态更改该位置值
因此,上面的rails迁移就是在数据库中为该位置创建元素
我做了一个测试
rails g migration add_position_to_products position:integer
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
我得到这个错误:
NoMethodError: undefined method `name' for #<Product:0x007ff4e7aa82a8>
这是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,不能是nil,正如您在上面看到的presence:true
我没有开发这些功能,看起来它们是在我开发编辑表单之后完成的。此时,我不确定如何处理堆栈溢出。错误/问题现已更改,请告知。我想我可以把雅各布的答案标记为正确的答案,因为通过提到产品模型,它让我明白了为什么我得到了未定义的名称方法
你能发布你的产品模型吗 我很高兴我的问题让你找到了解决办法!以防万一稍后有人看到这一页,我将尝试总结@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
类产品
更新产品。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
类产品
我看不到产品#名称
在任何地方使用。此代码现在是否引发该错误?您是否已完成重新加载
在控制台中?@RajMishra,即使在我重新加载之后,我仍然会出错。你能发布你的产品型号吗?请发布你的产品
型号和完整的错误消息,包括文件名、行号和完整的堆栈跟踪。@Ale,如果我对原始文件内容有重大错误,请告诉我,我会更新。