我有一个javascript bookmarklet,它曾经作为单用户模式工作。所以它过去的工作原理是,我在浏览器上点击我的bookmarklet,它就会在我的服务器上注入一个远程javascript。第二个javascript反过来向我的rails服务器发出ajax调用,代码如下所示:
$.post(
"http://192.168.1.2:3000/stuffs",
{stuff: JSON.stringify({"link":address})},
"json"
);当我在没有帐户的情况下处理我的项目时,这段代码可以正常工作。但今天我添加了一个巧妙的认证系统。所以现在系统有了用户。在我的控制器代码中我有
before_filter :authenticate_user!
def create
puts current_user
@stuff.user = current_user
...
end这不管用。current_user不返回任何内容。我只是想弄清楚是否存在现有的会话(是否有人已登录),然后想要在该用户下创建一个条目。在这种情况下,一个用户的东西和东西属于一个用户。站点的其余部分工作正常。似乎尝试使用bookmarklet访问并不能保留会话。
有人知道是怎么回事吗?
发布于 2011-11-17 19:53:36
我认为您需要将authenticity token参数传递给jQuery发送的params,如下所示:
$.post(
"http://192.168.1.2:3000/stuffs",
{
stuff: JSON.stringify({"link":address}),
authenticity_token: <%= "#{form_authenticity_token.inspect}" if protect_against_forgery? %>
},
"json"
);在这里可以找到更多关于form_authenticity_token的文档:http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection.html#method-i-form_authenticity_token
https://stackoverflow.com/questions/8164393
复制相似问题