我正在尝试让我的Google Chrome扩展程序使用Ajax将数据发送到rails网站上的ruby,并且无法弄清楚这里发生了什么:
我看到我的rails服务器已收到我正在发送的数据(两个阵列),但有404?然后我有关于用户ID的错误?
Started POST "/add_service" for 127.0.0.1 at 2014-01-24 00:34:37 +0800
Processing by UsersController#add_service as HTML
Parameters: {"urlArray2"=>["0.0.0.0", "item.jd.com", "stackoverflow.com", "www.google.com.hk"], "countArray2"=>["3670", "20", "2", "2"]}
User Load (11.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 404 Not Found in 13ms
ActiveRecord::RecordNotFound - Couldn't find User without an ID:
...
...
这是我的Chrome扩展程序中的Ajax:
jQuery.ajax({
type: "POST",
dataType: "JSON",
url: "http://0.0.0.0:3000/add_service",
data: ({'urlArray2':urlArray2,'countArray2':countArray2}),
success: function(){
//alert("Web services loaded into ConsultFred.");
}
});
这是我的用户控制器:
skip_before_filter :verify_authenticity_token, :only => [:add_service]
...
...
def add_service
logger.debug "This is working..."
#@user = current_user
#@user.sitearray = params[:urlArray2]
repsond_to do |format|
format.html { redirect_to root_url }
format.json { redirect_to root_url }
end
end
我的routes.rb(编辑:添加更多信息):
resources :users, :only => [:index, :show, :edit, :update ] do
post :share_to_linkedin, on: :member
end
post 'add_service', to: 'users#add_service'
[编辑1月24日]
现在看一下AJAX响应,它实际上是参数的问题,而不提供user.id号:
ActiveRecord::RecordNotFound at /add_service
============================================
> Couldn't find User without an ID
我怎么可能发送它呢? ajax请求是从chrome扩展程序外部的吗?
答案 0 :(得分:0)
我认为这些线条对你来说很重要:
User Load (11.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
Completed 404 Not Found in 13ms
ActiveRecord::RecordNotFound - Couldn't find User without an ID
您似乎没有正确地将参数传递给您的查询。应该将$1
作为实际值传递给SQL语句。