我几乎完成了为Ruby on Rails应用程序实现登录系统。当我登录或注销时,我的会话控制器将用户分别重定向到控制面板或主页。但是,当重定向到控制面板时,正文,标题等上的id/class标签不会被替换,因此控制面板专用的新HTML不会生效。在退出时重定向到首页时,通过AJAX加载到主页上的任何动态内容都不会像平常那样加载。我怎样才能解决这个奇怪的问题?Ruby on Rails - 登录或注销后的redirect_to不会执行与新页面相关的ajax/js/css加载
这里是我的会话控制器:
class SessionsController < ApplicationController
before_action :authenticate_user, :only => [:control_panel]
before_action :not_logged_in, :only => [:create]
# CREATE SESSION
def create
respond_to do |format|
if params[:username_or_email].blank? or params[:login_password].blank?
# Give feedback for incomplete form
flash[:feedback] = "Some fields were left empty."
format.js and return
end
if params[:username_or_email].include? '@'
user = User.find_by_email(params[:username_or_email])
else
user = User.find_by_username(params[:username_or_email])
end
if user && user.authenticate(params[:login_password])
session[:user_id] = user.id
format.html do
redirect_to ctrl_path, :notice => "Welcome back, #{user.username}" # Redirect to control panel
end
else
# Give feedback if credentials incorrect
flash[:feedback] = "The username, email, or password entered was incorrect."
format.js
end
end
end
def control_panel
# Control panel page
end
# DELETE SESSION
def destroy
reset_session
redirect_to root_path
end
end
任何意见或帮助是极大的赞赏!
编辑 如这里要求有一些自己的看法......
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= "Ian Hoffman | " + @title || "Railstest" %></title>
<%= csrf_meta_tags %>
<meta charset="UTF-8" />
<%= favicon_link_tag 'ruby.ico' %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= stylesheet_link_tag "https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,700|Open+Sans:400,400italic,700,300" %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
control_panel.html.erb
<html id="ctrl-page">
<head>
<% @title = "Control Panel" %>
</head>
<body>
<div id="wrapper">
<% if not notice.blank? %>
<h2 id="ctrl-notice"><%= "#{notice}" %></h2>
<% end %>
</div>
</body>
</html>
例如:<html id="ctrl-page">
没有被插入到“重定向”上,无论Rails的重定向的想法是什么。该标签保持为<html>
,就像在主页(由于原因未显示)。
你能也分享您的视图代码? – Milind
增加了一些意见 –
这可能是我的应用程序控制器覆盖的东西的错?当我重新加载页面时,ajax内容/不同的html显示正确。 –