Rails / RoR - NoMethodError(未定义的方法`destroy'代表nil:NilClass)

时间:2017-07-17 06:22:05

标签: ruby-on-rails validation destroy

我有一个TranslationsUser表和一个FavoriteTranslation表。我希望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

4 个答案:

答案 0 :(得分:0)

我想你差不多了。只需在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是否存在,否则translateUser没有favorite_translation,因此将允许创建新记录。

答案 1 :(得分:0)

如果您不想要验证,请不要担心,编写正确的关联将有助于您和 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关系,创建和更新有点合并在这里。

答案 2 :(得分:0)

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

 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

答案 3 :(得分:0)

我删除了这一行

public class B : IA {
    int IA.Val {get;set;}
}

internal interface IA {
    int Val {get;set;}
}

来自config.action_view.javascript_expansions[:defaults] = %w(jquery.min jquery_ujs) 以解决问题。