问题描述
我正在使用Can.js向Ruby on Rails服务器添加记录:
I am using Can.js to add a record to a Ruby on Rails server:
var Todo = can.Model({
findAll : 'GET /todos',
findOne : 'GET /todos/{id}',
create : 'POST /todos',
update : 'PUT /todos/{id}',
destroy : 'DELETE /todos/{id}'
}, {});
Todo.prototype.description = function() {
return this.name + " and " + this.complete;
};
Todo.findAll({}, function(todos) {
todos.each(function(todo) {
console.log(todo.name, todo.complete);
console.log(todo.description());
});
});
var todo = new Todo({name: "mow lawn"});
todo.save();
findAll()
实际上可以获取全部记录,但 save()
将插入空白记录。我怀疑它可能是 CSRF令牌
,但这显示为警告,并且如果它不想插入记录,则可能不会在该位置创建任何记录。全部,而不是添加名称为空的记录? (我也尝试过 var todo = new Todo({name: mow lawn,complete:true});
,结果是相同的。)
the findAll()
actually can get all the records, but the save()
will insert a blank record. I suspected it might be the CSRF token
, but that shows as a warning, and if it didn't want to insert the record, it probably won't create any record at all, instead of adding a record with name being nothing? (I also tried var todo = new Todo({name: "mow lawn", complete: true});
and it is the same result).
,但Rails服务器在终端上的打印方式为:
but the Rails server prints out on the terminal as:
Started POST "/todos" for 127.0.0.1 at 2013-04-12 08:16:05 -0700
Processing by TodosController#create as JSON
Parameters: {"name"=>"mow lawn"}
WARNING: Can't verify CSRF token authenticity
(0.2ms) begin transaction
SQL (0.8ms) INSERT INTO "todos" ("complete", "created_at", "name", "updated_at")
VALUES (?, ?, ?, ?) [["complete", nil], ["created_at", Fri, 12 Apr 2013 15:16:05 UTC
+00:00], ["name", nil], ["updated_at", Fri, 12 Apr 2013 15:16:05 UTC +00:00]]
(1.5ms) commit transaction
Completed 201 Created in 11ms (Views: 1.3ms | ActiveRecord: 2.5ms)
在Chrome开发者工具中,我在网络中看到:
In the Chrome developer tool, I see in Network:
Request URL:http://localhost:3000/todos
Request Method:POST
part of header:
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
form data:
name:mow lawn
如果我的操作是:
def create
p "params[:todo] is", params[:todo]
@todo = Todo.new(params[:todo])
respond_to do |format|
if @todo.save
format.html { redirect_to @todo, notice: 'Todo was successfully created.' }
format.json { render json: @todo, status: :created, location: @todo }
else
format.html { render action: "new" }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
然后终端将显示:
"params[:todo] is"
nil
即使终端显示标准的Rails日志:
even though the terminal shows the standard Rails log:
Parameters: {"name"=>"mow lawn"}
推荐答案
在Rails控制台输出中,您的 params
哈希看起来如下:
As shown in the Rails console output, your params
Hash looks like the following:
Parameters: {"name"=>"mow lawn"}
但是您的控制器正在创建一个新的Todo实例,使用以下内容:
But your controller is creating a new Todo instance using the following:
@todo = Todo.new(params[:todo])
:todo
不在 params
哈希中,因此 params [:todo]
为 nil
,基本上您的Todo实例没有传递给它的属性,因此它将 nil
保存为<$数据库中的c $ c> name 列。
:todo
is not in the params
Hash, so params[:todo]
is nil
and basically your Todo instance has no attributes passed to it, hence it is saving nil
for the name
column in the DB.
您的控制器应使用来解决此问题,这会将所有 params
哈希包装到 params [:todo]
子哈希。因此,如果您的控制器中有此容器:
Your controller should be using parameter wrapping to solve this, which will wrap all the params
Hash into a params[:todo]
sub-Hash. So if you have this in your controller:
class TodosController < ApplicationController
wrap_parameters :todo
...
end
您的 params
在执行操作之前,哈希将转换为以下内容:
Your params
Hash will be transformed to the following before the action is performed:
Parameters: {"name"=>"mow lawn", "todo" => {"name"=>"mow lawn"}}
然后,您现有的操作代码应该可以使用
And then your existing action code should work.
这篇关于can.js如何将记录添加到Rails服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!