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
精彩评论