English 中文(简体)
Devise在发布到不同路线时被注销
原标题:Devise being logged out on post to different route

我和迪维斯有一个非常奇怪的问题。我设置了一个同时接受获取和发布请求的路由。收到后,它会显示表格,在帖子上,它会提交表格。

当我向路由发送一个post XHR时,当它到达那里时,它会告诉我没有登录,并向我发送一个未经授权的401。之后我必须登录,然后我可以再试一次。

我已经想了好几个小时了,我所能想的只是我的控制器方法没有被调用。我在过滤器之前放入了自己的自定义auth,它只是确认,当我的rails应用程序被调用时,用户不再登录。

此外,如果我打开表格,但不提交,我可以继续正常工作。在XHR的某个地方,它正在设计将我注销。

如果你有什么想法,请帮忙,我不知道现在发生了什么。。。

谢谢

-斯科特

编辑:添加相关代码

路线.rb

match  projects/:p/filebox  =>  projects#show , :via => ["get","post"], :as =>  project_filebox 

项目_控制器.rb

before_filter :authenticate_user! # <--- By the time this gets called, the user is logged out
def show
# ^^^^ Doesnt get called. Logger shows that it recognized route though
logger.debug "-----------projects#show"
logger.debug "Current user logged in:"+user_signed_in?.to_s

正在提交的表单

<form class="upload" action="<%= project_filebox_path(@project) %>?n=7&cType=<%= cType %>&fid=<%= fid %>" method="post" enctype="multipart/form-data">
    <input type="file" name="file" multiple/>
    <button>Upload</button>
    <div>Add / Drag Files To Upload</div>
</form>

正在上载XHR的Javascript

formDataUpload = function (files, xhr, settings) {
            var formData = new FormData(),
                i;
            $.each(getFormData(settings), function (index, field) {
                formData.append(field.name, field.value);
            });
            for (i = 0; i < files.length; i += 1) {
                formData.append(settings.fieldName, files[i]);
            }
            xhr.send(formData);
        }

如果我错过了一些相关的代码,请告诉我

最佳回答

除了JS之外,这里没有太多内容,但有一个非常强大的变化,你遇到了问题,因为CSRF令牌没有作为请求的一部分设置。这在各种Rails 3.0.x版本中都发生了变化,所以如果没有代码,很难确定。

一个非常简单的测试是关闭CSRF(例如,从ApplicationController中删除protect_from_forgery)。如果它有效,你就有了答案,需要确保令牌被传递,否则你就要处理伪造保护。

问题回答

当我在一个页面上有两个表格时,我也遇到了同样的问题,其中一个张贴到申请内的路线,另一个则张贴到外部地址。John Paul Ashenfelter说得对,这与JS无关。

和你一样,我不想禁用整个网站的CSRF。我最终禁用了对张贴到控制器中的场外地址的方法的防伪造保护:

protect_from_forgery :except => [:some_method]

在造成CSRF问题的形式中:

<%= form_for :some_model, authenticity_token: false do%>




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签