【问题标题】:Rails error : undefined method `product' for #<User:0x00007faaa8c42700> Did you mean? products products=Rails 错误:#<User:0x00007faaa8c42700> 的未定义方法“产品”您是说什么?产品产品=
【发布时间】:2020-06-17 18:50:14
【问题描述】:

我正在尝试开发一个 Shopping application,它有 3 个模型,即 User(Devise)ProductBatch。我在UserProduct 之间建立了has_many 关联,并创建了User(signed up in Devise)。然后我将关联更改为has_and_belongs_to_many,并创建了一个迁移来创建连接表。我已经按照这个答案https://stackoverflow.com/a/57017241/9110386Product 添加到current_user。然后我删除了我的用户帐户并尝试注册,但它显示了这样的错误。 设计中的 NoMethodError::RegistrationsController#create 未定义的方法 `product' for # 你的意思是?产品产品=

用户模型:


class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  has_and_belongs_to_many :products, :dependent => :destroy
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates_length_of :product, maximum: 10
end

产品型号:

class Product < ApplicationRecord
  belongs_to :batch
  has_and_belongs_to_many :user
  validates :name, presence: true
  validates_associated :user

end

产品控制器

class ProductsController < ApplicationController
  before_action :authenticate_user!

  def index
    @products = Product.all
  end

  def show
    @product = Product.find(params[:id])
  end

  def new
    @product = Product.new
  end

  def edit
  end

  def create
  end

  def update 
  end

  def destroy
  end

  def add_cart
    product = Product.find(params[:product_id])
    #current_user.products << product
    #current_user.products << product unless current_user.products.include?(product)
    if current_user.products.include?(product)
      redirect_to products_path, notice: "Already in your cart"
    else
      current_user.products << product
      redirect_to products_path, notice: "Added to cart"
    end
  end

end

我在这里做错了什么。我还想通过从current_user 中销毁它来从购物车中删除Product。该怎么做?

提前致谢。

【问题讨论】:

  • 你是否修改了默认的 Devise::RegistrationsController?
  • 这行中出现的错误似乎是:validates_length_of :product, maximum: 10 in user model,users table model 是否有名为product的列?
  • @ricks Devise 没有控制器。如何修改默认的 Devise::RegistrationController?
  • @ricks 你能解释一下如何修改控制器,因为我找不到任何用于设计的控制器

标签: ruby-on-rails devise rubygems associations ruby-on-rails-6


【解决方案1】:

您在用户模型中留下了旧的验证。

在 app/models/user.rb 文件中删除这一行 validates_length_of :product, maximum: 10

【讨论】:

  • 谢谢。有效。我想从 current_user 中删除 Product 就像我通过 add_cartProductcurrent_user /b>。该怎么做?
  • @PranavRk 如果您想询问不同的主题,您需要提出新的问题,这样做是为了确保每个人都对所提出的特定问题保持领先。请创建一个新问题,详细说明您的需求
【解决方案2】:

您的错误是标记了 Devise RegistrationsController 的 create 方法。您可能在其中留下了对 user.product 的引用,而用户拥有复数产品。

【讨论】:

  • 谢谢,但我该怎么做,而且没有名为 Devise RegistrationController 的控制器。我必须自己为 Devise 创建控制器吗?
  • 好的。那时它可能不在里面。你能发布完整的堆栈跟踪,直到NoMethodError in Devise::RegistrationsController#create undefined method product' 为# 你的意思是?产品 products=`
  • Devise 有一个 rake 任务,它将为您创建设计控制器。但仅当您想自定义登录或注册流程时才需要这些。如果您还没有这样做;我的回答错了。
  • 是的。我没有创建设计控制器。为什么会抛出这个错误?
猜你喜欢
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2014-11-26
  • 1970-01-01
  • 2019-05-10
  • 2020-09-20
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多