我现有的sessions_controller使用内联javascript退出到root_path,如下所示:
render inline: "<script>window.location.replace('#{url}')</script>"
它在Rails 4中有效,但在Rails 5中没有。 大多数文档都在谈论在视图中调用javascript。 但这是控制器。 根据我的理解,我应该用
替换render :js => "myNewJavascriptFunction();"
我尝试过在另一个文件中创建myNewJavascriptFunction(),例如: views / sessions / create.js.erb它有错误
的未定义方法`myNewJavascriptFunction'
帮助:
这是我现有的代码: 在SessionsController中
def create
auth = request.env["omniauth.auth"]
user = User.from_omniauth(auth)
session[:user_id] = user.id
if params.permit[:remember_me]
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
# render :js => refresh(root_path) #not work
# redirect_to root_path #not work with JQM
render inline: "<script>window.location.replace('#{url}')</script>" #not work with Rails 5
rescue
render inline: "<script>window.location.replace('#{url}')</script>" #not work with Rails 5
end
视图/会话/ create.js.erb
function refresh(url='/') {
window.location.replace(\'#{url}\');
}
菜单调用
- if login?
%li
%a{"data-panel" => "main", :href => logout_path, "data-ajax"=>"false"} Sign Out
- else
%li
%a{:href=>new_session_path, "data-panel" => "main"} Sign In
%li
%a{:href=>new_identity_path, "data-panel" => "main"} Sign Up
%li
= link_to "Refresh", "#", :onclick=>"window.location.replace('/')"
- if !login?
= link_to image_tag( 'facebook-sign-in-button.png'), '/auth/facebook', rel: "external"
答案 0 :(得分:1)
无需拥有views / sessions / create.js.erb
只需将fill = TRUE
替换为render inline: "<script>window.location.replace('#{url}')</script>"