Search code examples
ruby-on-railsvalidationdestroy

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


I have a TranslationsUser table and a FavoriteTranslation table. I want a TranslationsUser to only have one favorite translation, however I do not want to use validations because I want my controller to replace a favorite translation if one already exists. Instead, I created a validate_uniqueness function.

When I try to replace a TranslationsUser with a new favorite translation, I get the following error in my terminal: NoMethodError (undefined method `destroy' for nil:NilClass):

I think the problem may be the before action :set_favorite_translation since it defines @favorite_translation

If so, how can I delete a specific FavoriteTranslation record: transUser.favorite_translations.first

If not, please help me figure out what the issue is! Many thanks.


    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

Solution

  • I tried the given solutions, but I found that the following worked. Thanks for all the help!!!!

     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