Ruby on rails 在登录后使用redirect_to(使用jquery mobile)时,标头中的Ruby逻辑无法正确工作
更新:添加时出现以下问题:Ruby on rails 在登录后使用redirect_to(使用jquery mobile)时,标头中的Ruby逻辑无法正确工作,ruby-on-rails,session,redirect,jquery-mobile,Ruby On Rails,Session,Redirect,Jquery Mobile,更新:添加时出现以下问题: <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 我的application.html.erb如下所示 <!DOCTYPE html> <html> <head> <%= yield :head %> </head> <body> <%= yield :
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
我的application.html.erb如下所示
<!DOCTYPE html>
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield :body1%>
</body>
我的登录页视图如下所示:
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to '/landingpage', :notice => "Logged in!"
<% content_for :head do %>
<% if current_user %>
<script>'function test(){alert('user is signed in')}'</script>
<% else %>
<script>'function test(){alert('user is not signed in')}'</script>
<% end %>
<% content_for :body1 do %>
<% if current_user %>
User is signed in
<% else %>
User is not signed in
<% end %>
<button onClick="test();">test</button>
<% end %>
'函数测试(){alert('用户已登录')}'
'函数测试(){alert('用户未登录')}'
用户已登录
用户未登录
测试
结果:登录后,页面显示“用户已登录”,但函数仍返回“用户未登录”。但是,如果在浏览器中单击“刷新”,则会正确加载所有内容
我试图理解为什么使用重定向不会更新标题信息。如果您有以下方法,它应该可以工作。你能粘贴你的
代码吗
您必须在布局中放置yield调用,每个视图的模板都是您放置块内容的位置
应用程序内/views/layouts/application.html.erb put
<!DOCTYPE html>
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield :body1 %>
<%= yield %>
</body>
然后在每个视图中,您都可以这样做:
<% content_for :head do %>
<h1>My Header!</h1>
<% end %>
<% content_for :body1 do %>
<h3>Body1</h3>
<% end %>
<h3>The rest of the page</h3>
我的头球!
车身1
这一页的其余部分
问题与jquery mobile有关,以下是有效的解决方案:
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<!-- Enter this section between the the .js links -->
<script type="text/javascript">
$(document).bind("mobileinit", function(){
$.mobile.ajaxEnabled = false;
});
</script>
<!-- End section -->
<script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script>
$(document).bind(“mobileinit”,function(){
$.mobile.ajaxEnabled=false;
});
好的,现在我得到了“Body1”和“页面的其余部分”。但是,:head不显示正确的信息。它似乎显示了我在重定向之前添加的数据。只需根据新发现重写问题即可。标题信息似乎不更新如何在应用程序\u controller.rb或帮助程序中获取当前\u用户?你能粘贴代码吗?