Controller#create 中的 NoMethodError - 未定义的方法“级别”...无法保存

问题描述 投票:0回答:1

我正在构建一个 ruby on rails 应用程序。 我在制作服装时遇到问题。 它抛出一个我不明白的错误

OutfitsController#create 中的 NoMethodError 未定义的方法“水平”为

但是我的任何代码中都没有命名级别..我不明白! 这是截图screenshot of the error,和我的代码。

 def create
    @user = current_user
    @outfit = Outfit.new(outfit_params)
    @outfit.user = current_user
    if @outfit.save
      redirect_to outfits_path
      flash[:notice] = "Outfit created"
    else
      flash[:notice] = "Something went wrong, please check the form again"
    end
  end

 private

  def outfit_params
    params.require(:outfit).permit(:name, :pet, :price, :color, :style, :size, :description, photos:[])
  end

  def update_outfit_params
    params.require(:outfit).permit(:name, :pet, :price, :color, :style, :size,:description,  photos:[])
  end

我尝试使用@outfit.save! 没有成功,它总是告诉我“NoMmethod 'level' ...”! 那是我不明白的,以前从未发生过..“水平”从何而来?

这是我的模型

class Outfit < ApplicationRecord
  belongs_to :user
  has_many_attached :photos

  SIZE = ['S', 'M', 'L']
  validates_inclusion_of :level, :in => SIZE

end

ruby-on-rails save nomethoderror levels
1个回答
0
投票

在你的模型中你有

class Outfit < ApplicationRecord
  belongs_to :user
  has_many_attached :photos

  SIZE = ['S', 'M', 'L']
  validates_inclusion_of :level, :in => SIZE

end

这里

validates_inclusion_of :level, :in => SIZE
正在调用一个名为level的方法。因此,要解决此问题,您可以创建一个名为 level 的方法,该方法返回可以在数组中检查的值,或者您可以将其更改为

validates_inclusion_of :size, :in => SIZE

这将解决错误。

© www.soinside.com 2019 - 2024. All rights reserved.