Rails/RoR - NoMethodError(nil:NilClass 的未定义方法“销毁”)

Rails/RoR - NoMethodError (undefined method `destroy' for nil:NilClass)

我有一个 TranslationsUser table 和一个 FavoriteTranslation table。我希望 TranslationsUser 只有一个最喜欢的翻译,但是我不想使用验证,因为我希望我的控制器替换一个最喜欢的翻译(如果已经存在)。相反,我创建了一个 validate_uniqueness 函数。

当我尝试用新的最喜欢的翻译替换 TranslationsUser 时,我的终端出现以下错误: NoMethodError (undefined method `destroy' for nil:NilClass):

我认为问题可能出在 before action :set_favorite_translation 因为它定义了 @favorite_translation

如果是这样,我该如何删除特定的 FavoriteTranslation 记录:transUser.favorite_translations.first

如果不是,请帮我看看是什么问题!非常感谢。


    before_action :set_favorite_translation, only: [:show, :edit, :update, :destroy]


 def create

    transUser = TranslationsUser.find(favorite_translation_params[:translations_user_id])


    @favorite_translation = FavoriteTranslation.new(favorite_translation_params)
    @favorite_translation.user_id = @current_user.id

    if validate_uniqueness(transUser) == false
        transUser.favorite_translations.first.destroy
    end

    respond_to do |format|
      if @favorite_translation.save
        #format.html { redirect_to @favorite_translation, notice: 'Translations users comment was successfully created.' }
        format.json { head :no_content }
      else
        format.html { render :new }
        format.json { render json: @favorite_translation.errors, status: :unprocessable_entity }
      end
    end

end

def validate_uniqueness(transUser)
      if FavoriteTranslation.joins(:translations_user).where('lang_id = ?', transUser.lang_id).where('favorite_translations.user_id = ?', @current_user.id).where('translations_users.translation_id = ?', transUser.translation_id).exists?
          return false
      else 
          return true
      end
 end

def destroy
    @favorite_translation.destroy
    respond_to do |format|
        format.html { redirect_to favorite_translations_url, notice: 'Translation was successfully destroyed.' }
        format.json { head :no_content }
  end
end


private
def set_favorite_translation
  @favorite_translation = FavoriteTranslation.find(params[:id])
end

我猜你快到了。只需在 destroy 方法中添加以下内容即可。

def destroy
    if @favorite_translation
      @favorite_translation.destroy
    end
    respond_to do |format|
      format.html { redirect_to favorite_translations_url, notice: 'Translation was successfully destroyed.' }
      format.json { head :no_content }
    end
end

这主要是检查 favorite_translation 是否存在,否则 translationUser 没有 favorite_translation,因此将允许创建新记录。

如果您不需要验证,不用担心,编写适当的关联将帮助您 DRY 您的代码。

首先,使用has_one :favorite_translationbelongs_to :translations_user联想,然后在你的create动作..

def create

  transUser = TranslationsUser.find(favorite_translation_params[:translations_user_id])

  @favTrans = transUser.favorite_translation || transUser.build_favorite_translation

  @favTrans.assign_attributes(favorite_translation_params)

  respond_to do |format|
    if @favTrans.save
      format.html { redirect_to @favTrans, notice: 'Translations users comment was successfully created.' }
      format.json { head :no_content }
    else
      format.html { render :new }
      format.json { render json: @favTrans.errors, status: :unprocessable_entity }
    end
  end
end

现在您不需要 destroy 记录和使用 has_many 关系,创建和更新在这里有点合并。

我尝试了给定的解决方案,但发现以下方法有效。感谢大家的帮助!!!

 def create
    transUser = TranslationsUser.find(favorite_translation_params[:translations_user_id])

    if validate_uniqueness(transUser) == true
        @favorite_translation = FavoriteTranslation.new(favorite_translation_params)
        @favorite_translation.user_id = @current_user.id
    else
        update
    end

  respond_to do |format|
    if @favorite_translation.save
      format.json { head :no_content }
    else
      format.html { render :new }
      format.json { render json: @favTrans.errors, status: :unprocessable_entity }
    end
  end
end


def update
    transUser = TranslationsUser.find(favorite_translation_params[:translations_user_id])
    @favorite_translation = FavoriteTranslation.joins(:translations_user).where('lang_id = ?', transUser.lang_id).where('favorite_translations.user_id = ?', @current_user.id).where('translations_users.translation_id = ?', transUser.translation_id).first
    @favorite_translation.translations_user_id = favorite_translation_params[:translations_user_id]
    @favorite_translation.save

    respond_to do |format|
      if @favorite_translation.update(favorite_translation_params)
        'Translations users comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render :edit }
        format.json { render json: @favorite_translation.errors, status: :unprocessable_entity }
      end
    end
end

我删除了这一行

config.action_view.javascript_expansions[:defaults] = %w(jquery.min jquery_ujs) 

application.rb 解决问题。