简体   繁体   English

在ajax调用后request.url不正确?

[英]request.url incorrect after ajax calls?

I login to my Rails application using a login page which is reached either directly through a login link that uses the route 我使用登录页面登录我的Rails应用程序,该页面可以通过使用该路线的登录链接直接访问

match 'ladmin/login' => 'ladmin#login'

or if I try to edit content. 或者如果我尝试编辑内容。
In either case I get taken to the login page, which allows me to login and then returns me to the page I was trying to use or the app index page, but now as a logged in user. 无论哪种情况,我都会进入登录页面,该页面允许我登录,然后将我返回到尝试使用的页面或应用程序索引页面,但现在是登录用户。

Most of the time this works totally ok, I get logged in and returns to my content edit page or to the links index page as expected (if I had just use the 'login' link iself). 在大多数情况下,此操作完全正常,我登录并按预期返回到我的内容编辑页面或链接索引页面(如果我只是使用'login'链接iself)。

However I have been able to track down a bug whereby 但是我已经能够找到一个错误

if I use either of my ajax links the request.url is remembered incorrectly going forward 如果我使用我的任何一个ajax链接,则request.url记住request.url

(to either toggle group shading or to toggle Summary/Details, then the next time (even if several clicks) that I try to login (assuming I am logged out initially) results in the blank page - though I am actually now logged in. Interestingly I notice that when this happens, the url that I end up at in the browser address bar is always localhost:3000/toggle_row_shading which seems like a clue to the problem - it seems like the request.url is remembered incorrectly . (以切换组底纹或切换“摘要/详细信息”,然后我下次尝试登录(即使多次单击)(假设我最初已注销)下的空白页面-尽管实际上我现在已登录。有趣的是,我注意到当发生这种情况时,我在浏览器地址栏中最终到达的url始终是localhost:3000/toggle_row_shading ,这似乎是问题的线索-好像request.url被错误地记住了。

I require digest/sha1 for authentication in the User model with various methods for authentication and password. require digest/sha1在用户模型中使用require digest/sha1进行身份验证,并使用各种身份验证和密码方法。

The relevant code would seem to be in my LadminController : 相关代码似乎在我的LadminController

  def login
    session[:user_id] = nil 
    if request.post?
      user = User.authenticate(params[:username], params[:password])
      if user
        session[:user_id] = user.id
        session[:username] = user.username
        uri = session[:original_uri]
        session[:original_uri] = nil 
        redirect_to(uri || {:action => "index", :controller => :links})
      else
        flash.now[:notice] = "Invalid username/password combination"
      end 
    end 
  end 

I can temporarily got around it with 我可以暂时解决

  def login
    session[:user_id] = nil 
    if request.post?
      user = User.authenticate(params[:username], params[:password])
      if user
        session[:user_id] = user.id
        session[:username] = user.username
        redirect_to({:action => "index", :controller => :links})
      else
        flash.now[:notice] = "Invalid username/password combination"
      end 
    end 
  end 

However this 'forgets' the page I have come from, eg an 'edit' link and just uses links#index to put me on after login which is not ideal. 但是,这“忘记”了我来自的页面,例如“编辑”链接,仅使用links#index在登录后将我放上去,这并不理想。 It does get around the blank page problem though, as the steps to reproduce it now longer make it happen. 但是,它确实可以解决空白页问题,因为现在复制它的步骤更长了,因此它可以实现。

How can I have the login return me to the intended edit page and not give me the blank page? 如何使登录名返回到预期的编辑页面而不给我空白页面?

btw my code for the ajax links is: 顺便说一句,我对ajax链接的代码是:

%a{href: '#', :data => {toggle_group_row_shading: 'toggle'}}
  click to toggle

Do I need an extra route perhaps as the app started at rails 2.3.8 ? 当应用程序从Rails 2.3.8开始时,我是否需要一条额外的路线?

My routes include 我的路线包括

match 'toggle_full_details' => 'links#toggle_full_details'
match 'toggle_row_shading' => 'links#toggle_row_shading'
get 'verify_link/:id', to: 'links#verify_link', as: :verify_link
get 'unverify_link/:id', to: 'links#unverify_link', as: :unverify_link

should I have them all as gets perhaps? 我应该把它们都当做吗?

My application controller includes: 我的应用程序控制器包括:

before_filter :authorize, :except => :login

and

def authorize
  unless User.find_by_id(session[:user_id])
    session[:original_uri] = request.url #request.request_uri
    flash[:notice] = "Please Log In!"
    redirect_to :controller => 'ladmin', :action => 'login'
  end
end

I have had this happen to me before. 我以前曾经发生过这种情况。 The problem I found was that for AJAX requests, the session[:original_uri] is still getting stored and... is bad. 我发现的问题是,对于AJAX请求, session[:original_uri]仍在存储中,并且...很糟糕。 Basically, it's storing a uri for an ajax request which doesn't display very well after logging in. The code I ended up with is something like: 基本上,它存储了一个ajax请求的uri,该URI登录后无法很好地显示。我最终得到的代码是这样的:

if request.get? && !request.xhr?
  session[:original_uri] = request.url
end

This way we're not storing into the session[:original_uri] things that shouldn't be redirected to after logging in. You may find other things in your app to add to this conditional. 这样,我们就不会将登录后不应该重定向到的内容存储到session[:original_uri]中。您可能会在应用程序中发现其他要添加到此条件中的内容。 For example, I had some download links that would render a send_file , but those were no good for storing for next login either. 例如,我有一些下载链接可以呈现send_file ,但这些链接也send_file存储下次登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM