[英]How to replace inline javascript in rails controller
My existing sessions_controller use inline javascript exit to root_path as follow: 我现有的sessions_controller使用嵌入式javascript退出root_path,如下所示:
render inline: "<script>window.location.replace('#{url}')</script>"
It worked in Rails 4 but not in Rails 5. Most document talking about calling javascript in view. 它在Rails 4中有效,但在Rails 5中无效。大多数文档都涉及在视图中调用javascript。 But this is controller. 但这是控制器。 From what I understand I should replace with 据我了解,我应该用
render :js => "myNewJavascriptFunction();"
Which I did tried create myNewJavascriptFunction() in another file eg: views/sessions/create.js.erb it has error 我曾尝试在另一个文件中创建myNewJavascriptFunction(),例如:views / sessions / create.js.erb,它有错误
undefined method `myNewJavascriptFunction' for 未定义的方法“ myNewJavascriptFunction”
Help: 救命:
Here my existing codes: In SessionsController 这是我现有的代码:在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
views/sessions/create.js.erb 意见/会话/ create.js.erb
function refresh(url='/') {
window.location.replace(\'#{url}\');
}
menu call 菜单调用
- 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"
No need to have a views/sessions/create.js.erb 无需查看/会话/create.js.erb
Just replace render inline: "<script>window.location.replace('#{url}')</script>"
with render js: "window.location.replace('#{url}')"
只需将render inline: "<script>window.location.replace('#{url}')</script>"
替换为render js: "window.location.replace('#{url}')"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.