开发者

rendering wrong view

开发者 https://www.devze.com 2023-02-12 22:58 出处:网络
There is a simple code: def new if !logged_in? respond_to do |format| format.js {render \"new_failed\"} return

There is a simple code:

def new
  if !logged_in?
    respond_to do |format|
      format.js {render "new_failed"}
      return
    end
  end

  article = Article.find params[:id]
  @comment = article.comments.build

  respond_to do |format|
    format.js
  end
end

When user in not logged in, "new_failed" should be rendered, but, actually, "new" action is rendered.

When i put the logged_in? to end of method:

def new
  if !logged_in?
    respond_to do |format|
      format.js {render "new_failed"}
      return
    end
  end

  article = Article.find params[:id]
  @comment = article.co开发者_JS百科mments.build

  respond_to do |format|
    format.html # new.html.erb
    format.js
  end
end

It works correctly.

What a strange behavior is it ? Can anyone explain it ?

Thanks.


The problem is the return inside the respond_to. If you do it like this it works (here).

if !logged_in?
  respond_to do |format|
    format.js {render "new_failed"}
  end
  return
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号