Rails渲染JavaScript显示文本 [英] Rails render javascript shows text
本文介绍了Rails渲染JavaScript显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不确定这种行为是否正常,但是当我这样做时:
I'm not sure if this behavior is normal or not, but when I do:
render js: "window.location.pathname = '#{new_user_session_path}';"
在我的控制器中,在执行该行并成功重定向之前,它会短暂显示文本 window.location.pathname ='/user/sign_in';
.
in my controller, it briefly displays the text window.location.pathname = '/user/sign_in';
before executing the line and redirecting successfully.
这是完整的控制器操作:
Here's the full controller action:
def vote_down
if user_signed_in?
current_user.vote_exclusively_against(@deal = Deal.find(params[:id]))
render :partial => 'deals/partials/interaction',
:locals => {:deal => @deal, :votes_for => @deal.votes_for, :votes_against => @deal.votes_against}
else
render js: "window.location.pathname = '#{new_user_session_path}';"
flash[:error] = 'Please sign in to vote.'
end
end
如何防止这种情况发生并直接呈现该模板?
How do I prevent this from happening and directly render that template?
推荐答案
为什么不仅仅使用rails控制器路由来重定向回new_user_session_path?
Why not just use the rails controller routing to redirect back to new_user_session_path?
respond_to do |format|
format.html { redirect_to new_user_session_path, error: 'Please sign in to vote.' }
end
这篇关于Rails渲染JavaScript显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文