为什么我在私有方法中返回 false 后我的 Rails 控制器没有停止执行?

发布于 2024-12-06 09:54:50 字数 826 浏览 1 评论 0原文

当我在以下控制器上运行 ajax POST delete 时,对于已删除的记录,rails 会引发错误“undefined method `destroy' for nil:NilClass”。但为什么当我尝试在 destory 调用之前的 find_follow 方法中呈现响应时,它仍然会这样说?执行不应该在find_follow中停止吗?

class FollowsController < ApplicationController


 def find_follow
   begin
     @follow = current_artist.follows.find(params[:id])
     raise "Record Not Found" if @follow.nil? 
   rescue => e
      respond_to do |format|
        format.html { redirect_to(artist_follows_path(current_artist),:notice => "#{e}") }
        format.js { render :text => "#{e}", :status => :not_found}
        format.json {render :json => "#{e}", :status => 400}
      end
      return false
    end
  end


  def destroy
    find_follow
    if (@follow.destroy)
       # respond_to html, js, json...

    end

  end
end

When I run ajax POST delete on my follows controller, for a record that has already been deleted, rails raises error "undefined method `destroy' for nil:NilClass". But why does it still say that when I tried to render the response in a find_follow method preceding the destory call? Shouldn't the execution stop inside find_follow?

class FollowsController < ApplicationController


 def find_follow
   begin
     @follow = current_artist.follows.find(params[:id])
     raise "Record Not Found" if @follow.nil? 
   rescue => e
      respond_to do |format|
        format.html { redirect_to(artist_follows_path(current_artist),:notice => "#{e}") }
        format.js { render :text => "#{e}", :status => :not_found}
        format.json {render :json => "#{e}", :status => 400}
      end
      return false
    end
  end


  def destroy
    find_follow
    if (@follow.destroy)
       # respond_to html, js, json...

    end

  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谢绝鈎搭 2024-12-13 09:54:50

您的 find_follow 返回 nil ,这很好,但是当您调用 destroy 方法时,您只需要在 destroy 方法中编写 return

尝试

def destroy
    return find_follow

您也可以使用 < code>before_filter 类似以下内容

class FollowsController < ApplicationController
 before_filter :find_follow, :only=>[:destroy]

 def find_follow
     @follow = current_artist.follows.find_by_id(params[:id])
     if @follow.nil? 
        redirect_to(artist_follows_path(current_artist),:notice => "#{e}")
     else
      return true
    end
  end


  def destroy
    if (@follow.destroy)
       # respond_to html, js, json...

    end

  end
end

Your find_follow returns nil which is fine but as you are calling destroy method you need to write return in destroy method only

Try

def destroy
    return find_follow

You can also use before_filter something like following

class FollowsController < ApplicationController
 before_filter :find_follow, :only=>[:destroy]

 def find_follow
     @follow = current_artist.follows.find_by_id(params[:id])
     if @follow.nil? 
        redirect_to(artist_follows_path(current_artist),:notice => "#{e}")
     else
      return true
    end
  end


  def destroy
    if (@follow.destroy)
       # respond_to html, js, json...

    end

  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文